Static variables and properties in AS3, an experiment
I was curious about the speed differences in AS3 between a static member of a class and an instance member. I frequent many forums and from a few different places I picked up the impression that static in actionscript is considered slower. However, when I went to confirm this I could see nothing.
So, I decided to test it myself. I set up a simple Profiler class that uses getTimer() to determine the interval between Start() and Stop(). I initially used this because I was testing ))GetAverageTime but(( the times returned were very small, on the order of 0.0002 ms... Yeah. I realize that I could do more complicate work to test it but I wanted a baseline that was easy to test against.
I came up with the test structure that you see below, and I figured it was a good way to start with.
for( i = 0; i < 1000000; ++i )
{
profiler.Start();
//... do something ...//
profiler.Stop();
}
average[index] = profiler.GetTotalTime();
index++;
profiler.Reset();
The only difference between each loop is what is done. For the baseline I just used
count = 5;, but the other tests were using a test class called StaticTest.StaticTest contains two public static members( one var, one const ) and two private instance members( one var, one const, both accesible through a get accessor. ) So, the
//... do something ...// became count = StaticTest.StaticConst; and so on.
I used the baseline to subtract from the other test and arrive at what the difference was.My results where not nearly deterministic enough for me, but they did show a trend.
| Type | 1st Run | 2nd Run | 3rd Run | |
| Static Const | 3.9 | 4.3 | 4.6 | |
| Static Var | 5.6 | 4.0 | 2.9 | |
| Instance Const | 6.2 | 5.8 | 5.3 | |
| Instance Var | 4.4 | 7.5 | 6.2 | |
| Type | Static Const | Static Var | Instance Const | Instance Var |
| 1st Run | 3.900 | 5.600 | 6.200 | 4.400 |
| 2nd Run | 4.3 | 4.0 | 5.8 | 7.5 |
| 3rd Run | 4.6 | 2.9 | 5.3 | 6.2 |
I'm in the process of attempting to come up with a more deterministic way to check this. As well, I intend to test static methods versus instance methods.

Post new comment