Welcome to Questionaries, where you can ask questions and receive answers from other members of the community.

Let us know at info@questionaries.org

Is it faster to count down than it is to count up?

8 like 0 dislike
Greeting  All,

Our computer science teacher once said that for some reason it is more efficient to count down that count up. For example if you need to use a FOR loop and the loop index is not used somewhere (like printing a line of N * to the screen) I mean that code like this :

for (i=N; i>=0; i--)  
  putchar('*');  

is better than:

for (i=0; i<N; i++)  
  putchar('*');  

Is it really true? and if so does anyone know why?
asked 2 years ago by pollard (41,990 points)

1 Answer

2 like 0 dislike
 
Best answer
Is it really true? and if so does anyone know why?

In ancient days, when computers were still chipped out of fused silica by hand, when 8-bit microcontrollers roamed the Earth, and when your teacher was young (or your teacher's teacher was young), there was a common machine instruction called decrement and skip if zero (DSZ). Hotshot assembly programmers used this instruction to implement loops. Later machines got fancier instructions, but there were still quite a few processors on which it was cheaper to compare something with zero than to compare with anything else. (It's true even on some modern RISC machines, like PPC or SPARC, which reserve a whole register to be always zero.)

So, if you rig your loops to compare with zero instead of N, what might happen?

    * You might save a register
    * You might get a compare instruction with a smaller binary encoding
    * If a previous instruction happens to set a flag (likely only on x86 family machines), you might not even need an explicit compare instruction

Are these differences likely to result in any measurable improvement on real programs on a modern out-of-order processor? Highly unlikely. In fact, I'd be impressed if you could show a measurable improvement even on a microbenchmark.

Summary: I smack your teacher upside the head! You shouldn't be learning obsolete pseudo-facts about how to organize loops. You should be learning that the most important thing about loops is to be sure that they terminate, produce correct answers, and are easy to read. I wish your teacher would focus on the important stuff and not mythology.
answered 2 years ago by william (91,210 points)
Counting from N down to 0 is slightly faster that Counting from 0 to N in the sense of how hardware will handle comparison..

Note the comparison in each loop

i>=0
i<N

Most processors have comparison with zero instruction..so the first one will be translated to machine code as:

   1. Load i
   2. Compare and jump if Less than or Equal zero

But the second one needs to load N form Memory each time

   1. load i
   2. load N
   3. Sub i and N
   4. Compare and jump if Less than or Equal zero

So it is not because of counting down or up.. But because of how your code will be translated into machine code..

So counting from 10 to 100 is the same as counting form 100 to 10
But counting from i=100 to 0 is faster than from i=0 to 100 - in most cases
And counting from i=N to 0 is faster than from i=0 to N

    * Note that nowadays compilers may do this optimization for you (if it is smart enough)
    * Note also that pipeline can cause Belady's anomaly-like effect (can not be sure what will be better)
    * At last: please note that the 2 for loops you have presented are not equivalent.. the first prints one more * ....
2 years ago by BloggeR (179,550 points)

Related questions

12 like 0 dislike
1 answer
11 like 3 dislike
3 answers
14 like 0 dislike
1 answer
9 like 0 dislike
1 answer
7 like 0 dislike
2 answers
asked 1 year ago by webmaster (25,380 points)