Local varibles considered for performance improvement

Local variables are available within the function body in which they are declared (delineated by curly braces).

As a matter of fact, lots of flash developers don’t invite local varibles into their actionscripts. One reason is that many AS programers are formerly designers who had no concept of varible scope and memory allocation.

But as recommended, the use of local vars can greatly boost the execution speed. Here’s how:

In a block of code of a local scope, add the varible declaration using the var keyword prior to any access to the varible, as is shown below:

var variableName:datatype;

The var keywork informs the compiler that the varible being declared is only needed in the local scope and flash player will actually use a much cheaper way to manipulate it in comparison to a nonlocal one.

The following test actully took me by surprise that the improvement is so significant!!!

The test program structure:

var startTime:Number = getTimer();
test();
trace(getTimer()-startTime);
function test() {
    /*do something here*/

}

For simplicity, I use a do-nothing for loop to benchmark the execution, notice the iteration varible named i.

—————————————————————————————–

function test() {
 for (i=0; i<99999; i++) {
 }
}

——-result——

290

—————————————————————————————–

function test() {
 for (var i=0; i<99999; i++) {
 }
}

——-result——

46

—————————————————————————————–

function test() {
 var i;
 for ( i=0; i<99999; i++) {
 }
}

——-result——

46

Why this happens?

In a particular scope, when a modification to an undeclared varible take place, the flash player actually tries to find the varible in outer scopes. If noting is found, it creats one. For specific reason, the player creates the varible as a member of containing MovieClip NOT a local one on the stack! As is known to all, access to a MovieClip member is much more expensive than access to a local varible allocated on the stack. The keyword var helps us create a local varible on the stack.

But if the var declaration occurs in a timeline scope, the varible is still treated as a member of MovieClip.

—————————————————————————————–

var startTime:Number = getTimer();
var i;
test();
trace(getTimer()-startTime);
function test() {
 for ( i=0; i<99999; i++) {
 }
}

——-result——

293

This is because that the timeline scope is not a local scope(not in a function body delineated by curly braces), it is somewhat like the class scope.

For more specific explanation to varible scopes, please consult the manual of Macromedia Flash About Varibles.

I’d like to mention something more about the effect of the varible name length on performance. Local primitive varible name length has no effect on performance becuase when a movie is published, no information of local primitive varibles’ name is retaind. But names of  nonlocal varibles tell a different story:

—————————————————————————————–

var startTime:Number = getTimer();
test();
trace(getTimer()-startTime);
function test() {
 for (anExtremelyLongVaribleName=0; anExtremelyLongVaribleName<99999;anExtremelyLongVaribleName++) {
 }
}

——-result——

340

Much slower indeed!

The best practice is:

  • use a local varible whenever possible
  • shorten varible names if worthwhile

Boogalah! It’s 12:22, I’m going for my PE lesson……

Advertisements
This entry was posted in Flash and ActionScript. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s