Loop increment myth busted

I was watching Lee Brimlow's screencast on using the AS3 Test Harness and he mentioned you can optimize a for var i loop by changing the incrementor from i++ to ++i (it's around the 10:00 minute mark). That was the second or third time I had heard that recently. Deep down it just didn't sound right but seeing is believing so I decided to use Grant's AS3 test harness and see for myself.

You can run the test with any interation value between 1,000 and 100,000.

 

Testing for (var i:int=0;i&ltxi++) vs for (var i:int=0;i<x;++i) loop performance.

 

These are the two loops that are being tested, nothing special.

 private function testLoop1():void 
{
var v:Number = 0;
var len:int = int(ns.value);
for (var i:int=0;i++)
{
v += Math.random();
}
}

private function testLoop2():void
{
var v:Number = 0;
var len:int = int(ns.value);
for (var i:int=0;++i)
{
v += Math.random();
}
}

As you can see from the results the difference is neglible at best when running loops with iterations upto 100,000. The variance between the two methods is very minor, fractions of a second, and seems to flucate in favour between the two options. Sometimes i++ is faster, sometimes ++i. Maybe as the iteration size increases the variance would increase in favour of the ++i however, the flash player starts to chug on loops with thousands of iterations where very much work is done so is there really any advantage to be gained, questionable.

6 Comments:

  1. Zack

    Thanks for testing and debunking more hocus pocus BS from Brimelow!

  2. Jeremy

    Don't be so quick to discount Lee's advance. The performance of these loops vary based on which version of the Flash player you use. The ++i style was indeed faster in flash 8 and 9.

  3. Derrick

    Jeremy, ran the same tests with Flash player 9 at 100,000 iterations .... no difference.

    loop[i++] (50 iterations)
    Player version: MAC 9,0,246,0 (debug)
    –––––––––––––––––––––––––––––––––––––––––––––––––––
    method..............................ttl ms...avg ms
    loop[i++] 1526 30.52
    –––––––––––––––––––––––––––––––––––––––––––––––––––

    –––––––––––––––––––––––––––––––––––––––––––––––––––
    loop[++i] (50 iterations)
    Player version: MAC 9,0,246,0 (debug)
    –––––––––––––––––––––––––––––––––––––––––––––––––––
    method..............................ttl ms...avg ms
    loop[++i] 1539 30.78
    –––––––––––––––––––––––––––––––––––––––––––––––––––

    No difference

  4. Zack

    Bam! Once again we see puerile sycophantism is no match for science and facts! Derrick you should start like a Flash Mythbusters blog for all this BS that the endlessly self promoting community 'wizards' are always spewing forth to puff themselves up. There's a lot of other 'optimization' junk out there just like this.

    This myth has been Busted!

  5. Jeremy

    Derrick, I stand corrected. I went back through my blog archives and found out I ran a similar test with 7 and 8 which showed some difference. But obviously my results are obsolete.

    Have you compared while vs for loops?

  6. Geoffrey McRae

    This is a hangover from C/C++, if the flex compiler is smart enough (which it seems to be), ++i and i++ should compile to the same byte code as it does in C/C++ if i is just a simple type.

    If i is a pointer to a complex type in C/C++, ++i is always faster.


Leave a comment

Name: (required)

Email: (required)

URL:

Captcha test: (required)
Comments: (required)

wrap code blocks in <code> </code> tags