Subscribe to RSS
get email updates
home | about | pixDif AIR app | video tutorials
polyGeek.com

Register for 360Flex in DC using the ad below and you will automatically be entered in a drawing for a free ticket. Read more.
place your ad here

Web Premium





Loop optimization

January 10th, 2007 . by polyGeek

Here’s a little nuance about loops that I never thought of before. If you are looping through an array you would probably write the code like this:

output:0 of 4 : Star Wars
1 of 5 : LOTR
2 of 5 : Matrix
3 of 5 : Superman
4 of 5 : Close Encounters

You’ll notice that I’m adding an element to the array which I’m looping through. That’s almost never a good idea. However I want to illustrate something with this. I add to the array after tracing the output so the first time through the loop the length is 4. Each time afterwards it was 5. And the for/loop noticed this.

Here’s the important thing: each time through the loop the for-conditions are rechecked. That means movies.length is evaluated over and over again. To optimize the loop we could create a variable that holds the value of the array length and then use that in the for-condition. That way the array length is only evaluated once.

The change would look like this:

output:

0 of 4 : Star Wars
1 of 5 : LOTR
2 of 5 : Matrix
3 of 5 : Superman

You can see that the movies.length was still increased but the loop didn’t run through it because the variable is set before loop runs.

If the array that you are looping through is going to change during the loop then you must use the first approach. But it’s unlikely that you’ll do that often, or hopefully ever, so go with the second case and you’re code will run faster.

While you’re at it you can optimize even further by looping through the array backwards. The Flash VM can count backwards through a loop faster than it can forwards.

If something here has proved valuable to you then feel free to drop a couple of bucks in the tip-jar.

Post to Twitter Post to Delicious Post to Facebook Post to Reddit Post to StumbleUpon


similar posts

7 Responses to “Loop optimization”


comment number 1 by: Keith Peters

Another reason to loop backwards is when you DO need to change the array within the loop. For example, say you needed to remove all elements of an array whose value was “killme”. You’d need to loop through the array, but you’d also need to change the array. When you remove items from the array, your indexing gets messed up if you are moving forwards. but backwards is like:

for(var i:Number = items.length-1; i >= 0; i–)
{
if(items[i] == “killme”) items.splice(i, 1);
}

If I wrote that correctly (i’m rushing…) it should remove the elements correctly, and not mess up the indexing in the process.

comment number 2 by: Josh Tynjala

Lately, I’ve been trying to remember to grab the array length before looping. I don’t know if it helps much, but for some reason I feel better doing it.

I would also like to add a thank you to Keith for his example. That’s a great trick, and I’ll do my best to remember to use it in the future. Thanks, man!

comment number 3 by: polyGeek

Good example.

You know, you and I are the kind of guys who would sit around a camp fire and talk about “when it’s a good idea to loop backwards through an array.” :-)

My wife is rolling here eyes as she reads this.

comment number 4 by: Phillip Kerman

Of all the code-optimization tips, storing an array’s length in a variable is the biggest performance boost. You can apply the rule of threes here: if you expect to access a calculated value (like a clip’s _x or an array’s length) 3 or more times, store it in an array. Perhaps the inverse is easier to understand: if you only plan to access a variable’s value 1 or 2 times, no sense storing it in a variable in the first place.

comment number 5 by: Dustin Senos

Great tip. This is especially effective when checking the length of long strings.

You can see some other performance tips with times on this great site:

http://www.oddhammer.com/actionscriptperformance/set3/

Cheers,

Dustin Senos

comment number 6 by: polyGeek

Phillip, “rule of three” sounds like something for Lord of the Rings but I like it. :-)

I think that in many instances it can also aid in code readability to create local variables.

comment number 7 by: polyGeek

Dustin, thanks for the link. Good stuff.

   Welcome back (Change)

Leave a Reply

comment feed RSS   subscribe to this comment thread

Recent Posts

   



polyGeek.com

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

Better Tag Cloud