Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better to have decrementing loops? [closed]

Tags:

c

c#

vhdl

I remember years ago hearing that it is more efficient to have loops decrementing instead of incrementing especially when programming microprocessors. Is this true, and if so, what are the reasons?

like image 699
kasperhj Avatar asked Sep 08 '11 12:09

kasperhj


2 Answers

One thing that occurs off the bat is that the end condition on a decrementing loop is liable to be quicker. If you are looping up to a certian value, a comparison with that value is going to be required every iteration. However, if you are looping down to zero, then the decrement itself on most processors will set the zero flag if the value being decremented hits zero, so no extra comparison operation will be required.

Small potatoes I realize, but in a large tight inner loop it might matter a great deal.

like image 166
T.E.D. Avatar answered Sep 29 '22 03:09

T.E.D.


In c# it makes no difference to the efficiency. The only reason to have decrementing loops is if you are looping through a collection and removing items as you go.

like image 26
Ben Robinson Avatar answered Sep 29 '22 03:09

Ben Robinson