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 Run | 2nd Run | 3rd Run |
| Assignment | 0.3 | 0.4 | 0.3 |
| Static Const | 5.9 | 6.0 | 5.9 |
| Static Var | 5.8 | 5.6 | 5.8 |
| Instance Const | 6.4 | 6.4 | 7.0 |
| Instance Var | 6.1 | 6.6 | 7.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.

Post new comment