7,166
edits
(→Tips) |
(→Tips) |
||
Line 495: | Line 495: | ||
/* Now display the digits */ | /* Now display the digits */ | ||
} | } | ||
</syntaxhighlight> | |||
==== Fast loops ==== | |||
[http://embeddedgurus.com/stack-overflow/2009/03/efficient-c-tips-7-fast-loops/ Efficient C Tips #7 – Fast loops « Stack Overflow] | |||
<syntaxhighlight lang="text" enclose="div"> | |||
So how does this efficiency arise? Well in the count up case, the assembly language generated by the compiler typically looks something like this: | |||
INC lpc ; Increment loop counter | |||
SUB lpc, #10 ; Compare loop counter to 10 | |||
BNZ loop ; Branch if loop counter not equal to 10 | |||
By contrast, in the count down case the assembly language looks something like this | |||
DEC lpc ; Decrement loop counter | |||
BNZ loop ; Branch if non zero | |||
Evidently, because of the ‘specialness’ of zero, more efficient code can be generated. | |||
</syntaxhighlight> | </syntaxhighlight> | ||