flash language_api_primitive_types
New primitive types
In ActionScript 2, only the Number type existed to define a number, so to store a whole or decimal number the Number type was used:
var age:Number = 20; var speed:Number = 12.8;
No distinction was made between whole, decimal or negative numbers. ActionScript 3 now integrates three ways of presenting numbers:
- int: represents a whole 32 bit number (32 bit signed integer)
- uint: represents an unsigned 32 bit whole number (32 bit unsigned integer)
- Number: represents a 64 bit decimal number (64-bit IEEE 754 double-precision floating-point number)
Let’s note that the two new types int and uint don’t allow capitals unlike the Number type from ActionScript 2. An int variable type can contain an oscillating number between -2147483648 and 2147483648 :
// display : -2147483648 trace( int.MIN_VALUE ); // display : 2147483648 trace( int.MAX_VALUE );
An uint variable type can contain a whole oscillating number between 0 and 4294967295:
// display : 0 trace( uint.MIN_VALUE ); // display : 4294967295 trace( uint.MAX_VALUE );
Beware, ActionScript 3’s virtual machine keeps execution types, if we try to store a floating decimal number in an int or uint variable type the number is automatically converted by the virtual machine:
var age:int = 22.2; // display : 22 trace ( age );
We should note that the virtual machine rounds the number down to make a whole number:
var age:int = 22.8; // display : 22 trace ( age );
This automatic conversion is assured by the virtual machine proves to be quicker than the floor method of the Math class. In the following code, we round the number in a loop with the floor method. The loop needs 111 milliseconds:
var distance:Number = 45.2; var rounded:Number; var start:Number = getTimer(); for ( var i:int = 0; i< 500000; i++ ) { rounded = Math.floor ( distance ); } // display : 111 trace( getTimer() - start );
At present, we let the virtual machine manage the rounding of the number:
var distance:Number = 45.2; var rounded:int; var start:Number = getTimer(); for ( var i:int = 0; i< 500000; i++ ) { rounded = distance; } // display : 8 trace( getTimer() - start ); // display : 45 trace( rounded );
We get the same result in 8 milliseconds which means an execution which is 14 times quicker.
Beware, this tip in only valid for positive numbers.
In the following case we see that the floor method of the Math class doesn’t send back the same value as the int conversion by the virtual machine:
var distance:int = -3.2; // display : -3 trace(distance); var depth:Number = Math.floor (-3.2); // display : -4 trace( depth );
Taking the idea that a distance is always positive we can use the uint type which gives results similar to the int type in this particular case:
var rounded:uint;
Unfortunately, the uint type is recognized to be slower because a mathematical process is carried out. On the other hand the Number type is seen to be quicker than the int type when used for division. When we define a loop it is better to use an incremental int type variable:
var start:Number = getTimer(); for ( var i:int = 0; i< 5000000; i++ ) { } // display : 61 trace( getTimer() - start );
On the other hand, if we use a uint type the performance drops by nearly 400%:
var start:Number = getTimer(); for ( var i:uint = 0; i< 5000000; i++ ) { } // display : 238 trace( getTimer() - start );
Bear in mind that if you’re not sure it is best to use the Number type:
start:Number = getTimer(); for ( var i:Number = 0; i< 5000000; i++ ) { } // display : 102 trace( getTimer() - start );
With this we get a compromise in terms of performance between the int and uint type. In general, it is best to avoid the uint type.
The same optimization can be used to calculate the rounded up number. We prefer to leave the virtual machine to do the rounded down conversion then we add 1:
var distance:Number = 45.2; var rounded:int; var start:Number = getTimer(); for ( var i:int = 0; i< 500000; i++ ) { rounded = distance + 1; } // display : 12 trace( getTimer() - start ); // display : 46 trace( rounded );
By using the ceil method of the Math class we slow down performances by around 300%:
var distance:Number = 45.2; var rounded:Number; var start:Number = getTimer(); for ( var i:int = 0; i< 500000; i++ ) { rounded = Math.ceil ( distance ) + 1; } // display : 264 trace( getTimer() - start ); // display : 46 trace( rounded );
For more information on optimization, you can go to the following address:
http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/
Worth remembering
- The int type allows you to represent a 32 bit number
- The uint type allows you to represent a positive 32 bit number
- The Number typs allows you to represent a 64 bit decimal number
- It is advisable to use the int type for whole numbers to optimize performances
- It is not advised to use the uint type
- If you are not sure you should use the Number type
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42


