What is the fastest way to loop ?
We have an integer i, and we want to increment i until it reach REPS,
fastest way ?
1)
for(i= 0;i<REPS;i++)
2)
for(i=-1;++i<REPS;)
3)
for(i= REPS;--i;)
4)
for(i= 0;(i-REPS)>>31;i++)
5)
for(i= -1;(++i-REPS)>>31;)
6)
for(i= -1;(++i-REPS)>>>31;)
All those methods are equivalents, except for 3 where we decrease i instead of increasing it..
My results:
** UPDATE: I changed and increased the value of REPS in the tests to try to get a more stable profiling.
My 2 winners :
5) for(i= -1;(++i-REPS)>>31;)
6) for(i= -1;(++i-REPS)>>>31;)
What do you get ?
I get dramatically different results every time I run it on an Intel Core i7 with Mac OS X. Usually the first and fourth loops are slowest, but everything else is up in the air.
Jackson,
I updated the test to loop over more values… give it another try
I re-updated the test once to use the “accurate” profiling method described here:
(see speedloop2 button)
http://guihaire.com/code/?p=791
The fastest for me is: for(i= REPS;–i;) with 26 ms, vs the others: 34-43 ms.
On Win XP, Intel E6300 Dual Core CPU, 11.8.800 Release player.