UPDATE: These findings are not relevant in more recent versions of the player. Please see this article for more recent results, and to run the tests for yourself.
Sho just posted a really interested article on why he won’t be using the int data type very frequently in ActionScript 3. Up until reading it, I had believed that as in other languages, there were large performance benefits to using ints and uints appropriately in AS3. Turns out that isn’t the case.
I did some quick testing on this, which confirm Sho’s assertion that int is only slightly faster than Number in ideal circumstances, but the results from uint were much more surprising.
I did a simple test that ran through 16777215 iterations of a for loop, using int, Number, and uint types as the iterator variable. It did 50 passes, and calculated an average time per pass. This should be ideal conditions for using ints and uints. The results were really interesting:
So as Sho said, int is not much faster than Number, but it probably still makes sense for use as an iterator and in other obvious cases. But uint is ridiculously slow, and very erratic! The other types were very consistent when I ran this test multiple times (remember the results were from 50 averaged passes, and so should be consistent), but uint was all over the place. Some of the individual passes went as high as 500ms, and as low as 65ms.
My takeaway without more research: Avoid uint, unless you have a really good reason for forcing the type. Use ints where they make sense, but don’t agonize over the performance implications of int versus Number. Default to Number whenever you aren’t sure. Most importantly, don’t take things for granted – test and benchmark wherever you can – you might be surprised by the results!
It’s important to note that typing in general does result in huge speed benefits in AS3. Running the above test with an untyped iterator averages 380-430ms per pass (I even had to reduce the number of passes to 20 to avoid a 15 second timeout). It also has a lot of secondary benefits like compile and run-time type checking.
A huge thank you to Sho for posting about this – I’m glad I didn’t spend months or years of coding carefully choosing the best numeric type, thinking I was optimizing my code, when in fact I was gaining very little performance (or in the case of uint, actually harming performance).
I’ve done a few more tests, as per Sho’s suggestion, and thought I’d post the results (they are pretty close to what you’d expect based on Sho’s post). I used the same methodology as above, but inserted a small formula that was run each iteration. Here are the results:
Assignment ( var a:TYPE = 0; )
Statistically, they look the same, which is to be expected.
Assignment ( var a:TYPE = 0.5; )
Predictably, Number is faster for fractional assignments, as the value does not need to be converted to an integer.
Division ( var a:TYPE = i/2; )
Number is a much faster type for division, as expected from Sho’s post. uint trails badly.
Multiplication ( var a:TYPE = i*2; )
Similar results to division. I thought int might perform better as the value would never have to be converted to a float (whereas in division it would).
Addition ( var a:TYPE = i+2; )
As expected from the plain iterator test, int is slightly faster for integer addition.
Bitshift ( var a:TYPE = i<<1; )
int outperforms Number fairly handily in bit operations, this is likely because Number needs to be converted into an int to have bit operators applied. This is the only test so far that uint does passably well in (other than assignment).