H3XED

Actionscript 3: Unsigned integers (uint) are slow!

Nov 5, 2010   Programming   Nick Vogt   Comments
Please note that this post is over a year old and may contain outdated information.
Unsigned integers are integers that can be positive values only. They are useful to have in programming languages because they can hold a higher maximum positive value than a signed integer, without using additional memory. For example, int in Actionscript 3 is a 32-bit signed integer and can hold a range from -2,147,483,648 to 2,147,483,647, while uint is a 32-bit unsigned integer and can hold a range from 0 to 4,294,967,295. It is useful to use unsigned when dealing with large counters that will never be negative, as it gives you a higher maximum.

Logically, you would want to use uint when dealing with integers that will never be negative; however, Actionscript 3 throws us a curve ball. It turns out math operations on unsigned integers are noticeably slower than operations on signed integers (at least using CS3, haven't tested it in CS4 or CS5). Here's the performance test I ran for int:

var t1:int;
var t2:int = 2;
var t4:int = 4;

var firsttime = new Date();
for(var i = 0; i < 5000000; i ++)
{
   t1 = t4 / t2;
}
var secondtime = new Date();
trace(secondtime - firsttime);

The process time for this was between 250 ms and 266 ms. Now with uint:

var t1:uint;
var t2:uint = 2;
var t4:uint = 4;

var firsttime = new Date();
for(var i = 0; i < 5000000; i ++)
{
   t1 = t4 / t2;
}
var secondtime = new Date();
trace(secondtime - firsttime);

The process time for this was between 344 ms and 359 ms! Similar results were achieved using addition and subtraction operations.

So it would seem, at least for now, int is a better choice unless you absolutely need the extra range of an unsigned integer.
Share This Post
Twitter

Comments (0)

Share This Post
Twitter
H3XED © Nick Vogt   RSS   Policies   Twitter