AS3 loops, performance comparison

October 8, 2006 on 6:20 pm | In Actionscript | 13 Comments

Recently I did some benchmarks to determine the fastest loop in AS3. Here are the result:

  performance decrease in percent
empty loop / variable assigment in body
loop code AS3 AS2
for (i = 0; i < k; i++) {} 0*/7 0/0
for (i = 0; i++ < k;) {} 35/0 0/3
i = 0; while (i < k) { i++; } 5/6 0/2
i = 0; while (i++ < k) {} 35/3 0/3
i = k; while (i-- > 0) {} 5/0 5/1
i = k; while (i--) {} 20/0 5/5
for (each in o) compared to fastest loop 2800 30
for each (i in o) compared to fastest loop 45 N/A

So the main thing to keep in mind is that a for..in loop is now roughly 28 times slower than a standard integer loop - in AS2 the impact wasn't that big. Also don't overload the loop header, but put everything in the loop body instead, for example:

// slower
var k:int = 1000;
for (var j:int = k - 1, i:int = 0; i < k; j = i, i++)
{
}

// faster
var k:int = 1000;
var j:int = k - 1;
for (var i:int = 0; i < k; i++)
{
    j = i;
}

Another importing thing is integer casting when accessing array elements:
The second example is roughly 20% faster than the first one:

// i*2 gets promoted to Number
for (var i:int = 0; i < 10000; i++)
{
    a[i*2] = 0;
}

// Goes through fast path
for (var i:int =0; i < 10000; i++)
{
    a[int(i*2)] = 1;
}

This was pointed out on slide 26 ("Promotion of Numeric Types") in the paper
ActionScript 3.0 and AVM2: Performance Tuning by Gary Grossman.

Conclusion

So, in AS3 there isn't really that much of a difference between the loop variations. The benchmark results were made by iterating an empty loop with 10.000.000 iterations. From this point of view, it doesn't matter at all.

If you have many loops which only get or set values and do simple arithmetics, you will perhaps notice a difference. But you have to always look at the broader image and find the bottleneck of the application, and a loop is the last thing you would optimize at first place.

13 Comments

TrackBack URI

  1. it seems to me there is one big mistake in your actionscript code:
    flash opimise empty loops by bypassing them … so your benchmark result must by faulsed by this

    Comment by tooy — October, 9 2006 #

  2. hey tooy, I don’t know how smart the compiler is, but I have updated the table which compares an empty loop vs a non-empty loop.

    If flash skips empty loops during compilation or runtime my benchmark result would all have been 0ms. But I do get some result. For example the first AS3 loop takes about ~30ms, the loop with variable assigment is a little bit slower.

    Comment by mike — October, 9 2006 #

  3. yeap that’s not really a mistake or a litle one ;)
    0ms or not 0ms that’s the question.. but that’s a fact : flash compiler is a strange monster :)

    another litle thing:
    –i is faster than i– as ++i is faster than i++ (lite difference)

    but as you say they’r many others things to optimize before loops

    Comment by tooy — October, 9 2006 #

  4. Nice, you have some good optimizations points in there. I’ll be trying integer casting when accessing arrays in the FisixEngine… 20% faster eh?

    Comment by Oz — October, 10 2006 #

  5. i just reread the part about integer casting. Now, is that only faster when you manipulate i? (in the case that i was originally an int)

    Comment by Oz — October, 10 2006 #

  6. That’s what I understand, Oz. The i*2 returns a Number so you have to cast it back to an int. But if the code was originally just a[i]=0 then you’d be good.

    Comment by Paul K — October, 11 2006 #

  7. I think if you use[i + something] this could result in a number or a string (associative array), so by declaring (i + something) as an int flash knows in advance how to access the array element.

    Comment by mike — October, 11 2006 #

  8. I believe the fastest loop in Flash is actually

    var i = k;
    while (–i -(-1)) {}

    Predecrementation is less bytecode than postdecrementation and subtraction is less than addition. So, –i is faster than i– and minus negative one is faster than plus one.

    Comment by Steven — October, 16 2006 #

  9. [...] AS3 loops, performance comparison [...]

    Pingback by fladdict» ブログアーカイブ » AS3ループ速度比較表 — May, 7 2008 #

  10. [...] Though there are many similar articles regarding this they have mostly been directly focused on loops or Number types and this article won’t directly repeat these tests as I mostly discovered the [...]

    Pingback by Tips on how to write efficient AS3 » Lost In Actionscript - Shane McCartney — September, 28 2008 #

  11. [...] just went through a post from polygonal labs that shows the difference in performance between several loops in AS3. If you go have a look at his [...]

    Pingback by AS3 loops performance benchmarking — December, 30 2008 #

  12. Coding Rules ActionScript 3.0 _ Neopets…

    1.1 General Rules The Game must be published as Flash 10 / Actionscript 3. Sound files may not be used on any timeline. In order to allow users to turn sound effects on and off,……

    Trackback by Confluence: Nickelodeon — February, 2 2010 #

  13. Coding Rules ActionScript 3.0 _ Neopets…

    1.1 General Rules The Game must be published as Flash 10 / Actionscript 3. Sound files may not be used on any timeline. In order to allow users to turn sound effects on and off,……

    Trackback by Confluence: Nickelodeon Habitarium — April, 29 2010 #

Sorry, the comment form is closed at this time.

Proudly powered by WordPress Theme based upon Pool theme by Borja Fernandez.
Entries and comments feeds.