Int, uint and number data type conversion

June 6, 2007 on 10:49 pm | In Actionscript |

There has been a discussion in the past which data type is the fastest in AS3, or how to improve performance by using certain data types in different situations. The answer is actually very simple: performance loss occurs only when converting between data types. This is demonstrated in the loop body below, where a variable of one type is assigned to the other:

var a:uint = 0;
var b:uint = 1 << 28;

var t:Number = getTimer();
for (var i:int = 0; i < 1e+7; i++)
{
	a = b;
}
trace(getTimer() - t);

The loop above computes in 56ms on my machine. The same loop using only signed integers or numbers for a and b takes exactly the same time:

//int only
var a:int = 0;
var b:int = 1 << 28;
...

//number only
var a:Number = 0;
var b:Number = 1 << 28;
...

Now, when var a and b is of mixed type one type has to be promoted or demoted to the other type:

//number -> uint is ~1.8x slower
var a:uint;
var b:Number;
a = b;

//uint -> number is ~2.3x slower
var a:Number;
var b:uint;
a = b;

//number -> int is ~1.8x slower
var a:int;
var b:Number;
a = b;

//no work has to be done here
var a:Number;
var b:int;
a = b;

This also holds true when performing arithmetics, but not for the uint type. According to the documentation, the sum of two integers is an integer, the sum of two numbers is a number. Adding two unsigned integers needs a data type conversion first:

//~1.2x slower
var a:uint;
var b:uint;
var c:uint = a + b;

//no difference with:
var a:int;
var b:int;
var c:int = a + b;

//or:
var a:Number;
var b:Number;
var c:Number = a + b;

I have chosen the value 1 << 28 because André Michelle posted that getQualifiedClassName() outputs different results depending on the number of bits. So in the very first code snippet with the loop body variable b seems to be of number data type:

var b:uint = 1 << 28;
trace(getQualifiedClassName(b)) //outputs Number

But because the first loop performs exactly the same as the ‘int to int’ or ‘number to number’ version, a reasonable answer would be that there is something wrong with getQualifiedClassName() instead of variable b being suddenly a number.

As a rule of thumb: Avoid mixing data types, always use signed integers for everything possible (e.g. counting variables, array access..), numbers if you need precision and unsigned integers only for ARGB color values. Avoid using uints for counting variables or arithmetic expressions.

8 Comments »

RSS feed for comments on this post. TrackBack URI

  1. [...] when reading another excellent polygonal labs post on benchmarks and performance where he wisely advises to use signed int for loops or iterations rather than uint where needed [...]

    Pingback by [ draw.logic ] AS3 Interesting Numeric Storage Behavior « — June, 7 2007 #

  2. Amusingly, in AS2, using the generic Number data type, the loop takes over 26 seconds on my machine :D

    Comment by Matthew Mayer — June, 7 2007 #

  3. Very interesting, thank you very much :)

    Comment by Thibault Imbert — June, 8 2007 #

  4. [...] Michelle: Weird behavior of numbers in as3 Michael Baczynski: Bitwise gems - fast integer math Michael Baczynski: Int, uint and number data type conversion Sho Kuwamoto: Avoid ints in ActionScript Grant Skinner: Types in AS3: ints not so fast, uints slow! [...]

    Pingback by Tenegri’s blog » Blog Archive » Are there real int and uint types? — May, 3 2008 #

  5. Hi, I just discovered your weblog, and it is very interesting.

    In your last paragraph, you advise only to use signed integers for ARGB colour values — but don’t you mean unsigned integers?

    Thanks for you interesting posts anyway!

    Comment by Niels — August, 26 2008 #

  6. Niels you are right, I meant unsigned integers.

    Comment by Michael — September, 14 2008 #

  7. [...] Explains the int, uint and number data types and which to use and when [...]

    Pingback by Flash and the art of optimization « whitenoise — June, 14 2009 #

  8. Hey Matthew, have you seen this article? You guys came up with different conclusions. Can you confirm these numbers (no pun intended)?

    http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/

    Comment by Steven — September, 17 2009 #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Proudly powered by WordPress Theme based upon Pool theme by Borja Fernandez.
Entries and comments feeds.