Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Is it really true? and if so does anyone know why?</p> </blockquote> <p>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 <strong>decrement and skip if zero</strong> (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.) </p> <p>So, if you rig your loops to compare with zero instead of <code>N</code>, what might happen?</p> <ul> <li>You might save a register</li> <li>You might get a compare instruction with a smaller binary encoding</li> <li>If a previous instruction happens to set a flag (likely only on x86 family machines), you might not even need an explicit compare instruction</li> </ul> <p>Are these differences likely to result in any <em>measurable improvement</em> on <em>real programs</em> 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.</p> <p>Summary: <strong>I smack your teacher upside the head!</strong> You shouldn't be learning obsolete pseudo-facts about how to organize loops. You should be learning that <strong>the most important thing about loops is to be sure that they <em>terminate</em>, produce <em>correct answers</em>, and are <em>easy to read</em>.</strong> I wish your teacher would focus on the important stuff and not mythology.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload