PDA

View Full Version : Number vs Int vs uint


Incrue
07-26-2006, 12:19 PM
http://kuwamoto.org/2006/06/15/avoid-ints-in-actionscript/
Now, this is really confusing, and i am totaly lost about how to know when use Number or not
I mean, besides loops, what int is good for?Nothing?

Incrue
07-26-2006, 12:31 PM
Well, i am a bit new at all this and i may saying something idiot but...
It occours to me that in this kuwamoto code, ints may be slower cos he makes ints made something they are not designed to do, so i change this:
j = (j + 15) / 7;
to this:
j = (j + 15) ;
and i get...
int version: 78msn
Number version: 141ms

jodieorourke
07-26-2006, 12:37 PM
My understanding is as follows: A variable typed as a Number is useful if you're unsure of the value you'll be storing in it. You can store both integers and floating points in Number, whereas you can only store integers in int. The value of a counter should always be an int, so typing a variable in this manner will always remove the likelihood of it returning anything other than an integer (however unlikely this might actually be). Essentially though, you could type your counter as either an int or Number, and both would work fine.

I think this is certainly more a case of 'best practises', rather than an explicitly right or wrong way of doing something.

jodieorourke
07-26-2006, 12:39 PM
Well, i am a bit new at all this and i may saying something idiot but...
It occours to me that in this kuwamoto code, ints may be slower cos he makes ints made something they are not designed to do, so i change this:
j = (j + 15) / 7;
to this:
j = (j + 15) ;
and i get...
int version: 78msn
Number version: 141ms

To be fair, those results look more realistic than the ones posted on his site. Generally, you would expect Number to be slower.

senocular
07-26-2006, 02:22 PM
int in AS isnt that much faster. There are certain considerations for int that have to be accounted for when used in Flash that prevent it from being that much faster than number.

The general concensus is, use int when you're not dealing with division or multiplication by decimal values. Generally, they're best used for values used with +/- such as increment variables in loops. Number is used for everything else but color which (esp when dealing with Alpha) you should use uint - in fact thats about the only time you should use uint.

Tink
07-26-2006, 09:28 PM
I would use int whenever i need a positive integer, uint whenever i need a interger and Number everywhere else.

Its not so much about the speed, but using the correct type for the value is has to hold.

Fall_X
07-27-2006, 08:53 AM
I would use int whenever i need a positive integer, uint whenever i need a interger and Number everywhere else.

Its not so much about the speed, but using the correct type for the value is has to hold.
So you would actually sacrifice speed (*) in favour of using the correct type? :confused:

(*) uint is slow : http://www.gskinner.com/blog/archives/2006/06/types_in_as3_in.html

Tink
07-27-2006, 10:01 AM
Yes i would rather my app throw up errors and therefore reduce risk in development, then have that tiny bit of extra speed.

And i wouldn't really say slow. Yes its fractionally slower but

I did a simple test that ran through 16777215 iterations of a for loop, using int, Number, and uint types as the iterator variable.

int: 24-26ms
Number: 31-36ms
uint: 105-225ms

So in a loop running 16777215, uint is on average 0.1 second slower. Are you ever going to loop 16777215? We are talking very small amounts of time here.

http://www.tink.ws/blog/use-the-correct-type-for-the-job/
http://www.bit-101.com/blog/?p=810

Fall_X
07-27-2006, 10:16 AM
Yes i would rather my app throw up errors and therefore reduce risk in development, then have that tiny bit of extra speed.

And i wouldn't really say slow. Yes its fractionally slower but



So in a loop running 16777215, uint is on average 0.1 second slower. Are you ever going to loop 16777215? We are talking very small amounts of time here.

http://www.tink.ws/blog/use-the-correct-type-for-the-job/
http://www.bit-101.com/blog/?p=810
That's a good point, and for applications I would use the correct type. But when making a game, I like to do everything possible to make the game run as fast as possible - even if it's tiny things like this that probably won't make much of a difference. A lot of small optimizations might meen the game runs good on a slightly slower machine than it would without the optimizations.

That being said, with AS3's better speed, the bottleneck would usually be the renderer anyway, so optimizations like this probably won't matter that much.

Flash Gordon
07-20-2007, 10:45 PM
I'm glad this was talked about here.

I would think that uint should be the fastest number type. Is there are reason as to why it is the slowest?


Multiplication ( var a:TYPE = i*2; )
int: 78-129ms
Number: 39-64ms
uint: 207-280ms


Just doesn't make any sense. What was supposed to be an speed increase is a waste of CPU.

Assertnfailure
07-20-2007, 11:41 PM
Yes i would rather my app throw up errors and therefore reduce risk in development, then have that tiny bit of extra speed.
Well, then picking the right numerical type for variables won't help you, as it won't throw an error when you assign an invalid number....instead it just translates it behind the scenes and then you end up with confusing "bugs."

For example, try assigning a negative value to a uint and see what happens ;)

I'm with senocular....only use uint for colors.

Flash Gordon
07-21-2007, 03:17 AM
that was a year ago.....I dug up the grave.......

Jim Freer
07-21-2007, 01:14 PM
If you were able to, or had to, write at the machine level and produced optimized code the int would beat everybody with the uint close behind. (At least a few years ago this was true. I havenít looked at these processors in a while.) A good compiler would produce the same results. The problem is we are using a language that was designed to be an interpreter.

I imagine many places where we are trying to make it an int or uint operation it ends up getting converted to a Number internally. As time goes on Adobe may make changes in their code that will reduce these conversions so the benchmarks may change. The drawback to this is that it puts more burden on the programmer to handle problems like watching for overflows in intermediate expressions.

http://freerpad.blogspot.com/

senocular
07-21-2007, 02:19 PM
+ - / * % are all Number operations. If used on an int/uint, they will be converted to a Number by the Flash player in the background and recast back to int/uint after the operation is complete

| & ^ >> << >>> (bitwize) retain the int/uint types in the player and are optimized for them for those types.

Assertnfailure
07-21-2007, 06:15 PM
+ - / * % are all Number operations. If used on an int/uint, they will be converted to a Number by the Flash player in the background and recast back to int/uint after the operation is complete

| & ^ >> << >>> (bitwize) retain the int/uint types in the player and are optimized for them for those types.

But isn't int++ faster than Number++?

EatJason
12-13-2007, 01:27 AM
I nearly started doing all math doing ints, and using old school fixed point math. It appears by this link:

http://www.gskinner.com/blog/archives/2006/06/types_in_as3_in.html

that ints are also better for addition, but don't have optimized multiplication or division. If there was some way to force the machine to do int division (don't think its even in flash 9) I think doing fixed point math would be better.

For now it looks like if you keep to the | & ^ >> << >>> operators, and possibly + and -, ints are better. So if you are making a tile engine and have to do alot of integer math, it might be a decent optimization.

Processors take care of floating point math really fast now a days, so usually what used to be done in C++ with fixed point math now don't bother with it because floats are fast enough to do most anything you want (unless you are doing some crazy stuff like falling sand engines)