[Show/Hide Right Column]

Neil Petrick Help

Blogs > Neil Petrick > Static variables and properties in AS3, an experiment
By VengantMjolnir293 points  on Sat 28 of Nov., 2009 15:10 PST

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.
Type1st Run2nd Run3rd Run
Static Const3.94.34.6
Static Var5.64.02.9
Instance Const6.25.85.3
Instance Var4.47.56.2


TypeStatic ConstStatic VarInstance ConstInstance Var
1st Run 3.900 5.600 6.200 4.400
2nd Run4.34.05.87.5
3rd Run4.62.95.36.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.

Barcode Clock