Okay, that was fast

January 11th, 2007 . by polyGeek

I’ve been messing around with code optimization. Like creating a variable to hold an array.length instead of using the array.length in a for/loop.

I’ll post some details later when I’m pretty sure I know what I’m talking about but essentially I’m iterating through an array with a length of 100,000 - [”0″,”1″, “2″,…”99,999″].

Anyway, I thought I’d try it out when compiled as AS3, just for the sake of comparison. After a few minutes of converting the code - I’m just learning AS3 now - I get it to run without errors. I create a variable to hold the current Date, count through the loop, and then get the Date again and trace the difference between the two dates to the output panel. But my output keeps coming up as zero. I check the code again. Can’t find anything wrong. What the frak?

Then I add another 0 to the count when creating the array so that it contains all the numbers between 0 and 999,999 - that’s a length of one-million. Run the code again and get an output of 16.

So the problem was that counting through an array of just one-hundred-thousand didn’t take a millisecond.

Okay, that was fast.

Just for giggles I ran it up to a length of ten-million. That took 125 milliseconds. I tried the same thing back in AS2 but got the beloved warning that the script is taking too long and would I like to abort. So I dropped it down to a length of one-million and that took 656 milliseconds.

So, in this trivial instance AS3 is running 6,560 times faster than AS2. Nice work guys.

Correction:

The AS3 code is only running 52-times faster than the AS2 code. I forgot to divide by the time it took the AS3 code to run - 125 milliseconds.

Here’s my math in case I’m screwing it up again: it takes the AS2 code 656 milliseconds to run through the loop 1-million times. I get a timeout before it will go up to ten-million so lets just multiply the result of the 1-million loop by 10 and assume that would be how long it would take: 6.56 seconds.

The AS3 code takes 125-milliseconds to run through the 10-million loop. 6560 / 125 = 52.48. So the AS3 code is only running 52.48 times faster.

Sorry about the mistake.

Now I’m somehow disappointed that AS3 is only 50-times faster than AS2. Like that’s a small thing.

You can download the code here. Note: you’ll need the Flash9 Alpha to republish the AS3 code.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Digg
  • Facebook

4 Responses to “Okay, that was fast”

  1. comment number 1 by: andy makely

    Also: using a while loop that counts backwards can often buy you a little more speed when iterating through an array.

    var i:int = myArray.length;
    while (i–){
    //do something with myArray[i];
    }

  2. comment number 2 by: polyGeek

    @Andy, Yes, counting backward using a while-loop seems to be the fastest method. I got that from IdleHamster.

  3. comment number 3 by: Josh Tynjala

    It’s interesting that 10 million iterations brought up the script warning dialog in AS2, but 1 million finished in less than a second. Isn’t it supposed to happen after 15 seconds? Sounds like it might be an issue in the older VM.

    Regardless, the new virtual machine screams. :)

  4. comment number 4 by: polyGeek

    @Josh, Thanks for casting doubt. I went back and checked my math and made the correction. However, I did double check and the AS2 code times out while trying to count to 10-million. It doesn’t take 15 seconds. I think the timeout is 5 seconds but that’s just a guess.

    I uploaded the FLAs for people to play with themselves. I’ll try to make something nicer in a few days.

Leave a Reply

Name

Mail (never published)

Website

- Why ask? This confirms you are a human user!

   




© Copyright 2008 polyGeek.com / Dan Florio, All Rights Reserved Except Where Explicitly Stated
Web Developement Blogs - Blog Catalog Blog Directory
M2 Websites