[Show/Hide Right Column]

Neil Petrick Help

Blogs > Neil Petrick > Static variables and properties in AS3, further experiments
By VengantMjolnir293 points  on Sat 28 of Nov., 2009 19:35 PST

Static variables and properties in AS3, further experiments

Earlier today I ran some tests on static versus instance members in actionscript. There were interesting results to me, and if you are interested they are here. After a bit of thought I ran into the conclusion that maybe getTimer() isn't very reliable. It is only millisecond precision so asking it to accumulate sub-millisecond tasks could very well be asking for trouble. Of course, flash doesn't have a more precise timer and I wasted the better part of a day finding that out. Still, it should be close enough as long as I'm sampling a larger action than a single operation.
Armed with this thunderbolt of a revelation I decided to remove the timer from the loop and sample the entire one million iterations as one. Of course, I needed a new baseline so I tested this against an empty loop.
Here are the new results, minus the time for the empty loop:( the empty loop was a consistent .3 milliseconds ) Also, the assignment operation was simply( which would have been flattened into a single operation with an optimizing compiler ):

 value = 5;

Type
1st Run2nd Run3rd Run
Assignment0.30.40.3
Static Const5.96.05.9
Static Var5.85.65.8
Instance Const6.46.47.0
Instance Var6.16.67.2

As you can see, MUCH more consistant. Now any profiling has to be taken with a grain of salt but from what is shown here it looks like there if very little difference. However, it seems that the 'static is slower than instance' appears to be wrong.
Now for the kick in the teeth (Grin). If you've been following up to this point you'll remember that I tested a 'public' static member against a 'private' instance member that had a public get function. Well I was curious just how much overhead this function introduced so I tested the same thing using a public var and public const.

Type
1st Run
Var 0.2
Const 0.3

WOW, what a difference eh? This would indicate that statics are indeed slower... 20 times slower!

Oh, and the last interesting thing I took out of this was: const for whatever reason seems slower than var.

Barcode Clock