Decrement versus Increment in Loop 2
Adobe’s “Optimizing Performance for the Flash Platform” recommends that “Use reverse order for while loops” in its “Miscellaneous optimizations” section. And in my last post, decrement was not explicitly faster than increment with the following code to compare them:
while (--i > -1)
while (++i < MAX_NUMBER) // MAX_NUMBER is a constant
Difference was very little in my Mac OS environment. But in Internet Explorer 7 and Firefox 3/Flash Player 10/Windows Vista, decrement was faster up to twenty percent than increment. After several trials, speed of operation between decrement and increment seems to be almost the same. The condition of loop does not care if expression is decrement or increment.
The difference came from value to be compared with a counter variable. The new sample script for increment below does not use a constant in the condition. Then its speed seems to be almost identical to the decrement's.
while (++i < <the literal number>)
4 Comments
What about this?
while (i–)
All this is obvious if you look to bytecode.
Use the Apparat (http://code.google.com/p/apparat/) to better understand how it works.
Thank you for your comment, Robert. I added the test and it is slower. Perhaps, I think implicit conversion of number to boolean might take cpu.
http://wonderfl.net/c/czez
Thank you for you comment, Anton. But what we would like to know is not the difference of the bytecode but logical explanation to tell which statements is faster than others.