Tuesday, May 6, 2008

AS3 Optimization

I am beginning to work on a new project for the basement that I am very excited about and am sure I will be posting about soon. However, to prepare for the project I have been doing research on code optimization.

I found a great document from Joa Ebert that goes through many optimization techniques that I was not aware.

Here are some of the notes I took from the document:
- Use int for Iterations. I've been using Number everywhere. For some reason I was under the impression that number was quicker. But Joa states that using ints during iterations as well as during array access are much quicker than using Numbers. In fact, he claims that casting a Number to an int when accessing an array is quicker than just using the number. Confused? Here is what I mean: Instead of array[i*2] to access an array. Use array[int(i*2)]. Note: i*2 becomes a number even if i is an int. Personally I would have thought that the casting would slow down the array call, but apparently not due to the fact that arrays are optimized for integers.
- Use Cast instead of "as" (at least outside of the debugger).
- Check for a null ahead of time instead of using a try/catch
- Use proper Types instead of generic Objects whenever possible, even when accessing an array. He states that Vector3D(array[i]).x is faster than array[i].x Again, I would have thought the cast call would slow it down, but it seems that giving Flash a direction is better than making it guess.

Interesting stuff. There were plenty more gems in that document and he explains each example in much more detail than myself so please take a look at the full document if you are interested.

2 comments:

Stephen said...

Hey, I have done a bunch of AS3 optimization tests myself http://www.stephencalenderblog.com/

hopefully it will save you some time ;)

~Stephen

Ickydime said...

Great posts Stephen. Lots of valuable information in your tests.

The one I found the most interesting was comparing hitPointTest to getPixel32... Will definitely need to keep that in mind when I jump back into my game engine.

Thanks man.