giovedì, agosto 28, 2008

Slow int, slower uint

I was just reading a weird bit of ActionScript 3 trivia at Grant Skinner’s blog: apparently both ints and units are slow in operations such as for loops. And that uints are particularly slow. I won’t pretend to fully grasp all of the entry and the article on kuwamoto.org that inspired it, but I definitely noticed an improvement when I changed from uint to int in a script I was working on.

“… I had believed that as in other languages, there were large performance benefits to using ints and uints appropriately in AS3. Turns out that isn't the case…I did a simple test that ran through 16777215 iterations of a for loop, using int, Number, and uint types as the iterator variable. It did 50 passes, and calculated an average time per pass. This should be ideal conditions for using ints and uints. The results were really interesting:

int: 24-26ms

Number: 31-36ms

uint: 105-225ms

“So as Sho said, int is not much faster than Number, but it probably still makes sense for use as an iterator and in other obvious cases. But uint is ridiculously slow, and very erratic!”

*

Adobe sort of seems to say this, too: “The int class lets you work with the data type representing a 32-bit signed integer….The int data type is useful for loop counters and other situations where a floating point number is not needed, and is similar to the int data type in Java and C++. “

Adobe says of uint: “The uint class provides methods for working with a data type representing a 32-bit unsigned integer. Because an unsigned integer can only be positive, its maximum value is twice that of the int class.”