Home » AS3

AS3 loops performance benchmarking

30 December 2008 5 Comments

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.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • E-mail this story to a friend!
  • LinkedIn
  • TwitThis

5 Comments »

  • intel core 2 6400 said:

    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

  • intel pentium 4 said:

    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

  • intel i7 quad core said:

    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

  • Stephen Matthews said:

    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

  • Thomas (author) said:

    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

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.

Security Code: