More on Number, int and uint in AS3

Shortly after the release of Flex 2, I noticed the discussion about Number, int and uint in AS3. While Macromedia is proud of the new primitive types int and uint, there are different voices.

 

What Macromedia says in ActionScript 3.0 overview:

"ActionScript 2.0 had a single numeric type, Number, a double-precision floating point number. One welcome addition to ActionScript 3.0 is the new int type—a 32-bit signed integer that lets ActionScript code take advantage of the fast integer math capabilities of the CPU. The int type is great for loop counters and almost anywhere a decimal point isn’t needed. Another new type is uint, an unsigned 32-bit integer type similar to int."

 

Quite the opposite, Sho suggests that we should Avoid ints in ActionScript. Then Grant Skinner carried out more tests and concludes that Types in AS3: ints not so fast, uints slow!

 

Read the articles and comments carefully, you’ll find the problem more interesting. The reasons for the test results goes deep into the implementation of ActionScript virtual machine.

 

I’m not annoyed by the performance degradation of int and uint much because I rarely have 16777215 iterations in a loop. I’m more interested in the types themselves because I am an AS language fan. I did another test focusing on the type information of which the result is quite surprising.

I use Flex Builder 2.0.143459 with Flash Player 9.0.16.0 on Windows Server 2003 R2.

The code:

var n :Number = 1.2;

trace("var n :Number = 1.2;");

showInfo(‘n’, n);

 

n += 1.8;

trace("n += 1.8;");

showInfo(‘n’, n);

 

var i:int = -1;

trace("var i:int = -1;");

showInfo(‘i’, i);

 

i = 1;

trace("i = 1");

showInfo(‘i’, i);

 

var ui:uint = 1;

trace("var ui:uint = 1;");

showInfo(‘ui’, ui);

 

ui = int.MAX_VALUE + 1;

trace("ui = int.MAX_VALUE + 1;");

showInfo(‘ui’, ui);

 

function showInfo(name:String, data:*):void {

trace("— " + name + " —");

trace("value: " + data);

trace("getQualifiedClassName: " + getQualifiedClassName(data));

trace("typeof: " + (typeof data));

trace("is int: " + (data is int));

trace("is uint: " + (data is uint));

trace("is Number: " + (data is Number));

trace("");

}

 

The out put is:

var n :Number = 1.2;

— n —

value: 1.2

getQualifiedClassName: Number

typeof: number

is int: false

is uint: false

is Number: true

 

n += 1.8;

— n —

value: 3

getQualifiedClassName: int

typeof: number

is int: true

is uint: true

is Number: true

 

var i:int = -1;

— i —

value: -1

getQualifiedClassName: int

typeof: number

is int: true

is uint: false

is Number: true

 

i = 1

— i —

value: 1

getQualifiedClassName: int

typeof: number

is int: true

is uint: true

is Number: true

 

var ui:uint = 1;

— ui —

value: 1

getQualifiedClassName: int

typeof: number

is int: true

is uint: true

is Number: true

 

ui = int.MAX_VALUE + 1;

— ui —

value: 2147483648

getQualifiedClassName: Number

typeof: number

is int: false

is uint: true

is Number: true

 

You see? is Number always returns true regardless of the declared type. typeof always results in "number". The result of is int and is uint depends on the actual value of the variable. e.g. When n holds an integral value 3, getQualifiedClassName returns int instead of the declared type Number. When ui holds 1, is int and is uint are both true, but when ui holds int.MAX_VALUE+1 which is out of the int range, is int becomes false.

 

At first, I guess that int and uint are not first class citizen in the type system but variations of Number. Why do they have lower cased initials while all class names in AS3 begins with upper cased letter? If you look in the Flex 2 debugger with "show type names" on, int and uint variables are displayed as Number. Maybe int and uint are Number indeed but with constraints and optimizations? Maybe Macromedia implemented int and uint in a tricky way so they seem to be normal types? Above is just my guess. One thing is true – Number, int and uint are undistinguishable. The result of is operator and flash.utils.getQualifiedClassName depends on the actual value more than the declared type.

 

Sho says, "ECMAScript, however, has a looser concept of numbers. The system is supposed to move smoothly from ints to doubles as needed. Because of this, virtually all math is done internally as Number, not as int." This might explain why  is Number returns true for both int and uint variables. But according to Macromedia, int is indeed a 32-bit signed integer, which is totally different from Number. So I guess that for non-integral arithmetics, int and uint variables are internally converted to Number and then converted back. This explains why int is faster in integral addition and shift but much slower than Number in multiplication and division.

 

So which type to use? I’m on the Use the Correct Type for the Job side. Optimization for performance is good but not the first thing in ActionScript programming. I mean Flash is mainly intended for RIA and runs as a browser plugin. So we should try to avoid CPU intensive code which may destroy user experience. Of course, some really cool visual effects do demand CPU and for them, optimization is necessary at all. To sum up, use int and uint whenever suitable. If performance really requires, use Number instead of int and uint.

 

Update 2006-7-1, 17:59 : About the benchmark

Many people have posted about their benchmark for int, uint and Number. I haven’t seen and run their testing code myself and I don’t know what version of compiler and Flash Player they are using. And my own benchmark seems inconsistent with some of the others’.

 

Only few tests are valid because there are a lot things happening behind the scene.  I mean only few tests can prove what they are used to demonstrate. From the ActionScript code you write to the bits actually gets executed by CPU, there are three phases that can have effects on performance – first the compiler, then the AVM, then the native machine. Each phase may optimize or alter the code in it’s own way. So in a lot of cases, your code does not run in the way it seems to. For example, try benchmark the following two functions.

private function test_whileUint():void

{

var x:uint = 0;

while (x++ < 10000000);

}

private function test_while2Uint():void

{

var x:uint = 0;

while (x < 10000000) x++;

}

 

They should behave exactly the same, shouldn’t they? But my test shows that the second version is more than two times faster than the first one. This is because the compiled byte code is different and finally the machine code is different. Benchmark writers should pay double attention to details. Such details can make your tests invalid. I believe many tests posted on the web should be rewritten -_- to reflect the truth.

 

Advertisements
This entry was posted in Flash and ActionScript. Bookmark the permalink.

One Response to More on Number, int and uint in AS3

  1. Unknown says:

    Hi,Do you have used LCDs, used flat screens and secondhand LCDs? Please go here:www.sstar-hk.com(Southern Stars).We are constantly buying re-usable LCD panels and working for LCD recycling.The re-usable panels go through strictly designed process of categorizing, checking, testing, repairing and refurbishing before they are re-used to make remanufactured LCD displays and TV sets.Due to our recent breakthrough in testing and repairing technology of LCD, we can improve the value for your LCD panels.
    Contact Us
    E-mail:sstar@netvigator.com
    website:www.sstar-hk.com

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