Pushing the boundaries

…couldn’t be any simpler!

int vs Number

Posted by vijayram on February 1, 2007

This issue was highlighted by Sho Kuwamoto sometime back who recommends to use Number where ever possible. Grant Skinner made some additional test and has posted his results here.

While looking through the code on tamarin on the class construct for Number, it is defined as a 64-bit double-precision floating-point number while, int and uint are 32-bit each. So technically both int and uint use 4bytes in the memory whilst Number uses 8 bytes of memory.

From, the Flex doc it says: “When you store integer values with the Number data type, only the 52 bits of the significand are used. The Number data type uses these 52 bits and a special hidden bit to represent integers from -9,007,199,254,740,992 (-253) to 9,007,199,254,740,992 (253)”. Explains the 64-bit range.

By looking through tamarin, it is evident what the maximum and minimum value you can store for each of these data types.
For Number:
public static const MIN_VALUE :Number = 4.9e-324
public static const MAX_VALUE :Number = 1.7976931348623158e+308
For int:
public static const MIN_VALUE:int = -0x80000000;
public static const MAX_VALUE:int = 0x7fffffff;
For uint:
public static const MIN_VALUE:uint = 0;
public static const MAX_VALUE:uint = 0xffffffff;

I was actually digging through the notes on tamarin just to see how things work and why we have this variations in speed.
It so happens that, almost all the numbers ranges that we use to code falls between the max and min values of an integer (-2,147,483,648 to 2,147,483,647). Flash does an implicit conversion at runtime for any values we pass between this range to an integer. Even if you pass a floating point to a variable data-typed as int.How did I know this?

The flash.utils package contains a method called describeType. Equipped with this function, call this on your variable which contains the value and you shall the result as follows:
<type name=”int” base=”Object” isDynamic=”false” isFinal=”true” isStatic=”false”>
<extendsClass type=”Object”/>
<parameter index=”1″ type=”*” optional=”true”/>

Even if your variable is data-typed to be a Number it still is converted to “int”. Now what happens if we exceeds the rage for Integer? It is automatically converted into a Number as it has a larger range than integer. Clever! What about uint? It is again converted to int.

Now go back to Grant Skinner’s test results and you shall see why int outperforms in most cases. As you are aware, Number data-type can accepts decimal values and hence floating-point operator(multiplication and division) is quicker when data-typed to Number.
So to avoid this performance overhead of implicit conversion, its better to data-type your variable to “int” during addition and subraction and whilst division and multiplication is involved use “Number” data-type.

Technorati Tags: , , ,


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: