hi
Does creating temporary variable inside for loop affects the execution speed.
Printable View
hi
Does creating temporary variable inside for loop affects the execution speed.
Try it and see. (I would expect the answer would depend on what you do with it, how impressive the variable is (a char vs. a gargantuan class object), and what your optimization settings are.)
Maybe no
Maybe yesCode:for ( i = 0 ; i < 10 ; i++ ) {
int j = i * 10;
}
It's generally not an issue for POD types.Code:for ( i = 0 ; i < 10 ; i++ ) {
myClassWithExpensiveCtorDtor j(i);
}
I had tried creating an int variable inside a for loop ,storing a value in the variable and then print it.But every time the program takes different time to complete execution.Quote:
Try it and see
creating a temporary variable inside a for loop is better or outside the for loop is better if the loop runs for many cycles.
Creating temporairy variables explicitly should not affect the execution speed. So the following should be the same:
In fact, I'd expect it to compile to the exact same code.Code:{
result+= array1[i]+array2[i]+array3[i];
}
{
int partialSum=array1[i]+array2[i];
result+= partialSum+array3[i];
}
Is this what you mean?
Thanks to all for replys,
I want to know wheater allocating temporary variables inside loop takes more cpu cycles(since pushing each time the varible in to stack and deleting when the scope is out) than allocating the variable ouside the loop.
In the second part of the code the variable partialSum has to be created when the execution enters the brace and it has to be destroyed when the variable goes out of scope,ie.taking more cycles than the first part of code.Code:{
result+= array1[i]+array2[i]+array3[i];
}
{
int partialSum=array1[i]+array2[i];
result+= partialSum+array3[i];
}
Does both parts of the code above takes same number of cpu cycles?
In either case you need a place to store the partial sum before the calculation. In the second case that place is named, that is all.
On really old C compilers it used to be that you had to use the register key word to make sure they are treated the same way, but you shouldn't do that on modern compilers.
You're taking this way too literally. This is a conceptual view of variables and has little to do with the code a compiler actually generates.Quote:
since pushing each time the varible in to stack and deleting when the scope is out
Indeed. To elaborate though:
It doesn't matter where inside a function a variable is declared, space for it is allocated upon entry to the function. The only sense in which a variable comes into existence during the function execution is that at some point its constructor must be run, unless of course it is a POD type in which case there is nothing extra to do.
This seems like a thing that the compiler would optimize if it thought that it was going to cause slow down. I usually initialize variables right before I use them as that is what Effective C++ recommends.
yes its slower than a for loop that uses a pre allocated variable, since one spends time allocating the variable and one does not, but the difference is on the order of a few clock cycles at most. I would avoid it if its part of an inner loop, but if its part of the outer loop, or a solitary loop, I wouldnt worry about it.