+ 5
Is their any difference in iterating a for loop from top to bottom and bottom to top in time? if so what is the reason?
The main Difference.
3 odpowiedzi
+ 9
I mean that if you want to print "1" 100 times using a for loop by iterating from 1 to 100 and 100 to 1, Will there be any difference in time taken for Execution.?
+ 2
Likely not. It's the same thing backwards.
However, using ++i instead of i++ can make a difference in execution time (We're talking nanoseconds).
for(int i = 0; i < 100; ++i){}
i++ must first keep track of the variable:
int temp = i;
Do the operation.
i = blah;
Then increment it.
i = i + 1;
Meanwhile ++i just increments.
i = i + 1.
So, you can generally expect ++i to be a faster operation.
+ 1
Uh.. top to bottom and bottom to top can mean many things.