Do single compilers opt for local and static variables?



  • For example, two samples of the code:

    int c=0;
    for (int i=0;i<100;++i) {...;++c;} 
    

    for (int i=0;i<100;++i) {...;static int c;++c;}
    

    Are there any differences in their speed?



  • First, these two fragments of the code are not equivalent.

    In this fragment of the code

    int c=0;
    for (int i=0;i<100;++i) ++c; 
    

    variable c has an external area of announcement in relation to the cycle.

    The same code fragment.

    for (int i=0;i<100;++i) {static int c;++c;}
    

    variable c has an advertisement area corresponding to the composite proposal of the cycle.

    So semantically, it's two different pieces of the code.

    To declare a rocky variable as static within the cycle &apos; s composite proposal only makes sense if the programme management passes through the cycle several times, and you need to calculate within the cycle some aggregate value for all cycle activations.

    But there's a problem there's no way this variable is visible outside the cycle. It is therefore difficult to find some meaningful application of this approach.

    Unless you can find a stunt like that somewhere in the field.

    Talking about some thumb optimization and comparing these two fragments of the code on a single criterion is pointless.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2