AS3 loops performance benchmarking

0 Flares Twitter 0 Facebook 0 Google+ 0 Pin It Share 0 Email -- Filament.io 0 Flares ×

I 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 post, you’ll see that the “for (i = 0; i < n; i++) {}” loop wins the first place.
I have set up a benchmarking tool just to see by myself… but the results were different.
Someone also posted a comment claiming that the ++i incrementing way is faster than the i++ one, so I put those in the test as well.
I also put the decrementing ways (i– and –i) too.

Here are my results for each loop with an iteration of 10000000 :

Loop type

time

results

for (i = 0; i < n; i++) {} 472 ms 10000000
for (i = 0; i < n; ++i) {} 438 ms 10000000
for (i = 0; i < n;) {i++} 364 ms 10000000
for (i = 0; i < n;) {++i} 418 ms 10000000
for (i = 0; i++ < n;) {} 372 ms 10000000
for (i = 0; ++i < n;) {} 385 ms 9999999
i = 0; while (i < n) { i++; } 381 ms 10000000
i = 0; while (i < n) { ++i; } 372 ms 10000000
i = 0; while (i++ < k) {} 386 ms 10000000
i = 0; while (++i < k) {} 399 ms 9999999
i = n; while (i > 0) { i–; } 194 ms 10000000
i = n; while (i > 0) { –i; } 191 ms 10000000
i = n; while (i–) {} 199 ms 10000000
i = n; while (–i) { } 191 ms 9999999
for (s in array) 24451 ms 10000000
for each (i in array) 1181 ms 10000000
accessing array without integer casting 2580 ms 10000000
accessing array with integer casting 2159 ms 10000000

The “results” column contains the values of an incrementing variable in the body loop showing the number of loops it has done. Sometimes you see that 1 loop is missing.

Conclusion

The while loops takes the first places in this test and it seems that decrementing instead of incrementing gives us even more power.

Those tests were run on a pretty old computer : AMD Athlon 2400, 1.5 Go Ram.
I ran the test on more recent computers and posted the results in the comments. As you can see, the more recent the computer is, the less variations you get.

But here are a few guidelines as mentionned in the polygonal labs post :

  1. Try to avoid the “for (s in array)” loop as it is 20 times slower than the “for each (i in array)” loop.
  2. When accessing values from arrays, cast the index value into integer ( int(i) ) when needed.  See examples below.

Slower:

v = a[i * 2 / 2];

Faster:

v = a[int(i * 2 / 2)];

The tool I used is right here for you to test. Keep in mind that the results might change from time to time and from computer to computer but what remains is the “i = n; while (–i) { }” loop always keeping the first place each time I ran the test. It’s by far the fastest loop.

Get Adobe Flash player

This was the first post about performance in AS3. Next one will cover arrays against linked lists.

0 Flares Twitter 0 Facebook 0 Google+ 0 Pin It Share 0 Email -- Filament.io 0 Flares ×

8 Comments

  1. intel core 2 6400

    intel core 2 6400 @ 2.13 Ghz, 3Go Ram :

    looping with : for (i = 0; i < n; i++) {}: 298 ms – results : 70000000
    looping with : for (i = 0; i < n; ++i) {}: 297 ms – results : 70000000
    looping with : for (i = 0; i < n;) {i++}: 297 ms – results : 70000000
    looping with : for (i = 0; i < n;) {++i}: 298 ms – results : 70000000
    looping with : for (i = 0; i++ < n;) {}: 297 ms – results : 70000000
    looping with : for (i = 0; ++i < n;) {}: 297 ms – results : 69999999
    looping with : i = 0; while (i < n) { i++; }: 297 ms – results : 70000000
    looping with : i = 0; while (i < n) { ++i; }: 297 ms – results : 70000000
    looping with : i = 0; while (i++ < k) {}: 299 ms – results : 70000000
    looping with : i = 0; while (++i 0) { i–; }: 297 ms – results : 70000000
    looping with : i = n; while (i > 0) { –i; }: 297 ms – results : 70000000
    looping with : i = n; while (i–) {}: 297 ms – results : 70000000
    looping with : i = n; while (–i) { }: 371 ms – results : 69999999
    looping with : for (s in array): 36143 ms – results : 70000000
    looping with : for each (i in array): 1553 ms – results : 70000000
    accessing array without integer casting: 2811 ms – results : 70000000
    accessing array with integer casting: 2082 ms – results : 70000000

  2. intel pentium 4

    intel pentium 4 1.7 Ghz, 1Go Ram :

    looping with : for (i = 0; i < n; i++) {}: 143 ms – results : 10000000
    looping with : for (i = 0; i < n; ++i) {}: 144 ms – results : 10000000
    looping with : for (i = 0; i < n;) {i++}: 134 ms – results : 10000000
    looping with : for (i = 0; i < n;) {++i}: 132 ms – results : 10000000
    looping with : for (i = 0; i++ < n;) {}: 136 ms – results : 10000000
    looping with : for (i = 0; ++i < n;) {}: 134 ms – results : 9999999
    looping with : i = 0; while (i < n) { i++; }: 134 ms – results : 10000000
    looping with : i = 0; while (i < n) { ++i; }: 136 ms – results : 10000000
    looping with : i = 0; while (i++ < k) {}: 141 ms – results : 10000000
    looping with : i = 0; while (++i 0) { i–; }: 128 ms – results : 10000000
    looping with : i = n; while (i > 0) { –i; }: 131 ms – results : 10000000
    looping with : i = n; while (i–) {}: 142 ms – results : 10000000
    looping with : i = n; while (–i) { }: 140 ms – results : 9999999
    looping with : for (s in array): 15073 ms – results : 10000000
    looping with : for each (i in array): 659 ms – results : 10000000
    accessing array without integer casting: 1432 ms – results : 10000000
    accessing array with integer casting: 1176 ms – results : 10000000

  3. intel i7 quad core

    intel i7 quad core 1.6GHz-2.8GHz, 8GB 1066MHz RAM

    looping with : for (i = 0; i < n; i++) {}: 39 ms – results : 10000000
    looping with : for (i = 0; i < n; ++i) {}: 53 ms – results : 10000000
    looping with : for (i = 0; i < n;) {i++}: 65 ms – results : 10000000
    looping with : for (i = 0; i < n;) {++i}: 66 ms – results : 10000000
    looping with : for (i = 0; i++ < n;) {}: 56 ms – results : 10000000
    looping with : for (i = 0; ++i < n;) {}: 71 ms – results : 9999999
    looping with : i = 0; while (i < n) { i++; }: 62 ms – results : 10000000
    looping with : i = 0; while (i < n) { ++i; }: 62 ms – results : 10000000
    looping with : i = 0; while (i++ < k) {}: 57 ms – results : 10000000
    looping with : i = 0; while (++i < k) {}: 64 ms – results : 9999999
    looping with : i = n; while (i > 0) { i–; }: 66 ms – results : 10000000
    looping with : i = n; while (i > 0) { –i; }: 55 ms – results : 10000000
    looping with : i = n; while (i–) {}: 58 ms – results : 10000000
    looping with : i = n; while (–i) { }: 64 ms – results : 9999999
    looping with : for (s in array): 2764 ms – results : 10000000
    looping with : for each (i in array): 215 ms – results : 10000000
    accessing array without integer casting: 228 ms – results : 10000000
    accessing array with integer casting: 128 ms – results : 10000000

  4. OSX 10.6.6
    2.16 GHz Intel Core 2 Duo
    3GB 667 Mhz DDR2 SDRAM
    Macbook Pro

    looping with : for (i = 0; i < n; i++) {}: 1 ms – results : 100000
    looping with : for (i = 0; i < n; ++i) {}: 1 ms – results : 100000
    looping with : for (i = 0; i < n;) {i++}: 0 ms – results : 100000
    looping with : for (i = 0; i < n;) {++i}: 1 ms – results : 100000
    looping with : for (i = 0; i++ < n;) {}: 1 ms – results : 100000
    looping with : for (i = 0; ++i < n;) {}: 1 ms – results : 99999
    looping with : i = 0; while (i < n) { i++; }: 1 ms – results : 100000
    looping with : i = 0; while (i < n) { ++i; }: 1 ms – results : 100000
    looping with : i = 0; while (i++ < k) {}: 1 ms – results : 100000
    looping with : i = 0; while (++i 0) { i–; }: 1 ms – results : 100000
    looping with : i = n; while (i > 0) { –i; }: 1 ms – results : 100000
    looping with : i = n; while (i–) {}: 0 ms – results : 100000
    looping with : i = n; while (–i) { }: 1 ms – results : 99999
    looping with : for (s in array): 56 ms – results : 100000
    looping with : for each (i in array): 4 ms – results : 100000
    accessing array without integer casting: 4 ms – results : 100000
    accessing array with integer casting: 3 ms – results : 100000

  5. Windows Vista
    Intel Core2 Duo E8400 @ 3.00Ghz
    4,00 Go RAM

    looping with : for (i = 0; i < n; i++) {}: 5 ms - results : 1000000 looping with : for (i = 0; i < n; ++i) {}: 15 ms - results : 1000000 looping with : for (i = 0; i < n;) {i++}: 4 ms - results : 1000000 looping with : for (i = 0; i < n;) {++i}: 4 ms - results : 1000000 looping with : for (i = 0; i++ < n;) {}: 4 ms - results : 1000000 looping with : for (i = 0; ++i < n;) {}: 4 ms - results : 999999 looping with : i = 0; while (i < n) { i++; }: 4 ms - results : 1000000 looping with : i = 0; while (i < n) { ++i; }: 4 ms - results : 1000000 looping with : i = 0; while (i++ < k) {}: 4 ms - results : 1000000 looping with : i = 0; while (++i < k) {}: 4 ms - results : 999999 looping with : i = n; while (i > 0) { i–; }: 4 ms – results : 1000000
    looping with : i = n; while (i > 0) { –i; }: 4 ms – results : 1000000
    looping with : i = n; while (i–) {}: 4 ms – results : 1000000
    looping with : i = n; while (–i) { }: 4 ms – results : 999999
    looping with : for (s in array): 261 ms – results : 1000000
    looping with : for each (i in array): 25 ms – results : 1000000
    accessing array without integer casting: 26 ms – results : 1000000
    accessing array with integer casting: 17 ms – results : 1000000

  6. AS3 - AIR

    would be nice to see some benchmarks with smartphones and tablets running an air appication

  7. Robert

    “results might change from time to time and from computer to computer but what remains is the “i = n; while (–i) { }” loop always keeping the first place each time I ran the test. It’s by far the fastest loop.”

    What in the hell are you talking about? Every single example in the comments disproves that rubbish.

    “I ran the test on more recent computers and posted the results in the comments.”
    You PROVED YOURSELF WRONG

  8. hi there
    thx for your very “engaging” comment.

    Well, I re-ran the test and I still got the “i = n; while (i > 0) { –i; }” being the second fastest just behind the “for (i = 0; i < n;) {i++}" with 1ms of interval. Not that bad I think. Now I would have appreciated your results as I guess you ran the test for yourself as well. That would have been a bit more constructive I think. And if you really wanted to think this a little bit through, you would have simply assumed that no program (no matter what it is) will ever behave the same on different computers. You also need to keep in mind that the flash player has improved quite a lot over the years, and techniques that had proven to be the fastest a couple of years (and even months) ago may not necessarily be the fastest anymore. Now it's up to you to take what has to be taken from that. cheers.

Leave a Comment

*