hi
Does creating temporary variable inside for loop affects the execution speed.
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); }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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.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?
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
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.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
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.since pushing each time the varible in to stack and deleting when the scope is out
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
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.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
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.