Are there any disadvantages to declaring a variable inside the loop body?

Suppose we have a loop that repeats itself many times:

for (int i=0; i < 1000000; ++i) {
  int s = 100;
  s += i;
  cout << s;
}

      

We are using s

inside the body of a loop, so ideally we would like to declare it there so that it doesn't pollute the surrounding namespace.

I am wondering if it has any drawbacks. For example, will it be performance-dependent because the program re-declares s

at every iteration?

+3


source to share


3 answers


Conceptually, this variable is built and destroyed at each iteration.

But does this affect performance? Well, you can check your case right here . Remove int

in line 7 to switch between local and local variables loop.
Conclusion: no difference. The assembly is the same!



So, just use what makes sense in your code. If you need one object for each iteration, make one object at a time. The optimizer is smarter than you think. If that wasn't enough, you'll come back to it with data profiling and meticulous customization rather than broad guidelines.

+7


source


Yes. Declaring a variable inside the loop will deconstruct and restore it on each iteration. This may not be noticeable with small loops and simple data types, which the compiler will optimize anyway, however, when working with complex objects and large loops, it is best to declare variables outside.

If the variables for the loop are using too much memory, you can wrap the loop and declarations in curly braces, causing all variables enclosed within the curly braces to be deleted upon exit. In most cases, such micro-optimizations don't matter, but if you are using complex classes and such, just use variable initialization outside and reset every time.



It is generally not a good idea to specify too many variables as this makes your code difficult to read and increases memory usage. If you can, don't declare variables when you don't need to. For example, your example could be simplified to for(int i = 0;i<1000000;i++)cout<<i+100;

. If such optimizations are possible and do not make your code difficult to read, use them.

+2


source


Destroying int is a noop. The variable ceases to exist, but there is no need to run runtime code.

References or pointers to variables that cease to exist have undefined behavior. Before initialization, newly created local variables are undefined. So just reusing an old variable for a new one is legal, the compiler doesn't have to prove that there are no such outstanding references.

In this case, if it can prove that the value was constant 100

, it might even skip everything except the first initialization. And it can do this initialization "early" as there is no definite way to detect that it is happening sooner. In this case it is easy, and most compilers will do it easily; in more complex cases, less. If you flagged it const

, the compiler no longer has to prove that it is not modified, but rather can accept it!

Many areas where C ++ breaks out of undefined exist to make some optimizations easier.

Now if you have something more complex like vector<int>{1,2,3,4,5}

destruction and creation becomes less noop. It is still possible to "pull" a variable out of the loop, but much more difficult for the compiler. This is because dynamic allocation is sometimes difficult to optimize.

+1


source







All Articles