Agnivesh Singh
Agnivesh Singh

Reputation: 537

Is looping faster than traversing one by one

Let us consider the following code snippet in C++ to print the fist 10 positive integers :

for (int i = 1; i<11;i++)

{

  cout<< i ;

}

Will this be faster or slower than sequentially printing each integer one by one as follow :

x =1;

cout<< x;

x++;

cout<< x;

And so on ..

Is there any reason as to why it should be faster or slower ? Does it vary from one language to another ?

Upvotes: 2

Views: 98

Answers (2)

phonetagger
phonetagger

Reputation: 7873

This question is similar to this one; I've copied an excerpt of my answer to that question below... (the numbers are different; 11 vs. 50; the analysis is the same)

What you're considering doing is a manual form of loop unrolling. Loop unrolling is an optimization that compilers sometimes use for reducing the overhead involved in a loop. Compilers can do it only if the number of iterations of the loop can be known at compile time (i.e. the number of iterations is a constant, even if the constant involves computation based on other constants). In some cases, the compiler may determine that it is worthwhile to unroll the loop, but often it won't unroll it completely. For instance, in your example, the compiler may determine that it would be a speed advantage to unroll the loop from 50 iterations out to only 10 iterations with 5 copies of the loop body. The loop variable would still be there, but instead of doing 50 comparisons of the loop counter, now the code only has to do the comparison 10 times. It's a tradeoff, because the 5 copies of the loop body eat up 5 times as much space in the cache, which means that loading those extra copies of the same instructions forces the cache to evict (throw out) that many instructions that are already in the cache and which you might have wanted to stay in the cache. Also, loading those 4 extra copies of the loop body instructions from main memory takes much, much longer than simply grabbing the already-loaded instructions from the cache in the case where the loop isn't unrolled at all.

So all in all, it's often more advantageous to just use only one copy of the loop body and go ahead and leave the loop logic in place. (I.e. don't do any loop unrolling at all.)

Upvotes: 3

Stack crashed
Stack crashed

Reputation: 220

In loop, the actual machine level instruction would be the same, and therefore the same address. In explicit statements, the instructions will have different addresses. So it is possible that for loops, the CPU's instruction cache will provide performance boost that might not happen for the latter case.

For really small range (10) the difference will most likely be negligible. For significant length of the loop it could show up more clearly.

Upvotes: 1

Related Questions