Since I've started my adventure with AS couple of years ago FlashPlayer's performance increased a lot, but still it is not enough for calculation intensive tasks. There is also a good news. Many performance issues can by fixed with source code optimizations. Without further ado here are some of my AS3 optimization tips.
-
Use Vectors instead of Arrays if possible.
-
If you know length of Vector before you create one in code pass it to Vector constructor.
-
If you know that length of Vector will be constant then pass true to Vector constructor as second argument making it fixed vector.
-
When you loop through Vector elements use variable of uint type. It is faster than int in FlashPlayer 10.2.
- When you want acces Vector element like that for example vector[i+1] change it to vector[uint(i+1)].
-
If it is possible try to do as small number of operations using 2D Vectors by which I mean Vectors accessible like that vector[i][j] they are at least 2x slower then 1D vectors. If for example you are looping through 2D vector then good practice is to assign every vector[i] to local variable and than access value for every "j". By doing that you should see at least 50% performance increase in data access from 2D Vector.
-
When incrementing value use ++i instead of i++ wherever possible.
-
Remove all nonessential code from loops.
-
When operating on uint or int variables whenever possible use bit operators for example instead i*4 use i<<2. More info about bitwise operators.
-
When you loop through vector elements don't use vector.length in loop condition instead assign this value to local uint variable and use it instead.
-
When using && or || operators don't mix types like that Number && int or uint || int. Instead if possible use that same type like that int && int or uint || uint it is up to 3,5x faster.
-
Using nested if statements is faster than using && operators in single if statement.
-
Assigning variable of type Number to variable of type int or uint is 2x slower than assignments of int to int or uint to uint or even uint to int.
-
When creating empty Array don't do this like that var arr:Array = new Array() instead do it like that var arr:Array = [] it's faster.
-
Using switch is about 15% slower on Windows XP and 10% slower on Mac OS X than if else statements. Beware of switches in performance-critical code!
I hope those tips will help you with you optimizations.
1 comment:
I wanted to thank you for this great read!! I definitely having fun with every little bit of it I have you bookmarked to check out new stuff you publish
Post a Comment