Great article on code optimization in flash actionscript, by FI
[ 2007-12-07 14:09:12 | Author: liuhuan ]
from: http://www.thinkswedish.com/#ContentHolder:feed=blog&Entries:0=entry+1=877refer to : http://www.thinkswedish.com/#ContentHolder:feed=blog&Entries:0=entry+1=869
Quote
In a previous post Bartek gave you some tips and tricks on how to write faster Flash applications. To follow up on that I thought I would list some useful code optimizations.
Code optimization can make a big difference in processing intensive applications. I did for example double the performance of an AS3 particle system by applying the optimizations listed below. Nevertheless don’t over-emphasize optimization. Well structured and readable code is in most cases more valuable than optimized code. Write the bulk of your code first and optimize it only if it is necessary and when you know where the bottlenecks are.
In general you should consider optimizations like these when you have code with a lot of loops and iterations, code that is executed on every frame or code that is fired continuously by a timer. I won’t go into detail on why these practices improve performance as there are plenty of in-depth discussions available elsewhere. Take my word on it, Google it or test it your self. The optimizations apply to both ActionScript 2 and 3 unless otherwise stated.
Eight code optimization tweaks
Declare the types of all variables
Do not use array length inside loops or in loop headers
Avoid multiple array lookups
Use int for counters and array lookups (AS3 only)
Use Number for math operations. There are cases where int is faster but it is generally best to stick with Number (AS3 only)
Use multiplication instead of division
Access variables directly, i.e without using getter and setter functions
Declare many variables at once
Two AS3 implementations of the same loop
//Slow unoptimized loop:
for(var i = 0; i < arrayOne.length; i++){
if(arrayOne[i].enabled()){
arrayOne[i].getX() = arrayTwo[i].getX()/2;
arrayOne[i].getY() = arrayTwo[i].getY()/2;
arrayOne[i].getZ() = arrayTwo[i].getZ()/2;
}
}
//Fast optimized loop:
var length:int = arrayOne.length, i:int=0;
for(; i < length; i++){
objectOne:SomeObject = arrayOne[i];
objectTwo:SomeObject = arrayTwo[i];
if(objectOne._enabled){
objectOne._x = objectTwo._x*0.5;
objectOne._y = objectTwo._y*0.5;
objectOne._z = objectTwo._z*0.5;
}
}
Note: Class variables are usually prefixed with underscores to indicate that they are private. I left the underscores here to indicate that the variables are accessed directly and not through getter and setter functions.
Integration in Eclipse
If you use Eclipse and FDT as your development environment you can set up templates to easily create optimized statements. Here is an AS2 example that creates an optimized for loop that loops through all the elements in an array. Just copy-and-paste the code into the “fori” template in “Window->Preferences->FDT->Editor->Templates”:
var length:Number = ${array}.length, ${index}:Number=0;
for(; ${index}<length; ${index}++ ) {
${cursor}}
Now all you have to do is to type “fori” and hit ctrl+space and you’ll get this:
var length:Number = array.length, i:Number=0;
for(; i<length; i++ ) {
}
Optimization couldn’t be easier…
Code optimization can make a big difference in processing intensive applications. I did for example double the performance of an AS3 particle system by applying the optimizations listed below. Nevertheless don’t over-emphasize optimization. Well structured and readable code is in most cases more valuable than optimized code. Write the bulk of your code first and optimize it only if it is necessary and when you know where the bottlenecks are.
In general you should consider optimizations like these when you have code with a lot of loops and iterations, code that is executed on every frame or code that is fired continuously by a timer. I won’t go into detail on why these practices improve performance as there are plenty of in-depth discussions available elsewhere. Take my word on it, Google it or test it your self. The optimizations apply to both ActionScript 2 and 3 unless otherwise stated.
Eight code optimization tweaks
Declare the types of all variables
Do not use array length inside loops or in loop headers
Avoid multiple array lookups
Use int for counters and array lookups (AS3 only)
Use Number for math operations. There are cases where int is faster but it is generally best to stick with Number (AS3 only)
Use multiplication instead of division
Access variables directly, i.e without using getter and setter functions
Declare many variables at once
Two AS3 implementations of the same loop
//Slow unoptimized loop:
for(var i = 0; i < arrayOne.length; i++){
if(arrayOne[i].enabled()){
arrayOne[i].getX() = arrayTwo[i].getX()/2;
arrayOne[i].getY() = arrayTwo[i].getY()/2;
arrayOne[i].getZ() = arrayTwo[i].getZ()/2;
}
}
//Fast optimized loop:
var length:int = arrayOne.length, i:int=0;
for(; i < length; i++){
objectOne:SomeObject = arrayOne[i];
objectTwo:SomeObject = arrayTwo[i];
if(objectOne._enabled){
objectOne._x = objectTwo._x*0.5;
objectOne._y = objectTwo._y*0.5;
objectOne._z = objectTwo._z*0.5;
}
}
Note: Class variables are usually prefixed with underscores to indicate that they are private. I left the underscores here to indicate that the variables are accessed directly and not through getter and setter functions.
Integration in Eclipse
If you use Eclipse and FDT as your development environment you can set up templates to easily create optimized statements. Here is an AS2 example that creates an optimized for loop that loops through all the elements in an array. Just copy-and-paste the code into the “fori” template in “Window->Preferences->FDT->Editor->Templates”:
var length:Number = ${array}.length, ${index}:Number=0;
for(; ${index}<length; ${index}++ ) {
${cursor}}
Now all you have to do is to type “fori” and hit ctrl+space and you’ll get this:
var length:Number = array.length, i:Number=0;
for(; i<length; i++ ) {
}
Optimization couldn’t be easier…
Quote
Following up on the article published on Search-this.com, Bartek Drozdz gives a few tips and tricks on how to make your Flash application faster and more performant:
“One of the questions I was asked by Search-This was, “What is Flash’s biggest weakness?” The biggest weakness, I answered, is obviously the fact that it is slow. So here's some tips on how to make Flash8/AS2 applications a little bit faster.
Tip 1. Avoid bitmaps with alpha channel whenever possible. When two bitmaps containing an alpha channel are stacked on the stage, Flash has to do extra calculations to figure out the color of the pixel on every pixel where the bitmap contains alpha information. That adds a lot of computing.
Tip 2. Always draw/attach only those elements that are visible on the screen at the given moment, reuse movie clips whenever possible and try to control the number of clips on stage - the fewer the better.
Tip 3. Try to have as many graphical and textual assets imported to your FLA, and load externally at runtime only what is absolutely necessary.
Tip 4. Flatten graphical assets to minimize their number. If you have a button that is composed of a background, a shadow, an icon and a text label, the ideal solution is to export it from Photoshop as one flattened bitmap with all those elements in it (yes, even the text label!).
Tip 5. Optimize bitmaps in Photoshop not in Flash; the Photoshop save-for-web module is a lot better than Flash when it comes to optimizing bitmap files. In the FLA file, all the imported bitmaps should then have the lossless PNG/GIF compression option checked - Flash does not need to compress them any more.
Tip 6. The less ActionScript the better. Always minimize the amount of code you write. Especially do not create functionality that "will be useful in the future". In the future you almost always need something else that you prepared for anyway. Also, AS is for interactivity, not for building stuff (the stage is designed for that). So if half of your code consists of attachMovie calls, something is wrong.
Most of these tips will make you lose a good deal of flexibility during development - this is true, but it will greatly increase the speed of your application. And speed is more important than the comfort of work of a developer, isn't it? If you want flexibility, try to use Photoshop (JSX) and Flash (JSFL) scripts to automate your tasks.“
“One of the questions I was asked by Search-This was, “What is Flash’s biggest weakness?” The biggest weakness, I answered, is obviously the fact that it is slow. So here's some tips on how to make Flash8/AS2 applications a little bit faster.
Tip 1. Avoid bitmaps with alpha channel whenever possible. When two bitmaps containing an alpha channel are stacked on the stage, Flash has to do extra calculations to figure out the color of the pixel on every pixel where the bitmap contains alpha information. That adds a lot of computing.
Tip 2. Always draw/attach only those elements that are visible on the screen at the given moment, reuse movie clips whenever possible and try to control the number of clips on stage - the fewer the better.
Tip 3. Try to have as many graphical and textual assets imported to your FLA, and load externally at runtime only what is absolutely necessary.
Tip 4. Flatten graphical assets to minimize their number. If you have a button that is composed of a background, a shadow, an icon and a text label, the ideal solution is to export it from Photoshop as one flattened bitmap with all those elements in it (yes, even the text label!).
Tip 5. Optimize bitmaps in Photoshop not in Flash; the Photoshop save-for-web module is a lot better than Flash when it comes to optimizing bitmap files. In the FLA file, all the imported bitmaps should then have the lossless PNG/GIF compression option checked - Flash does not need to compress them any more.
Tip 6. The less ActionScript the better. Always minimize the amount of code you write. Especially do not create functionality that "will be useful in the future". In the future you almost always need something else that you prepared for anyway. Also, AS is for interactivity, not for building stuff (the stage is designed for that). So if half of your code consists of attachMovie calls, something is wrong.
Most of these tips will make you lose a good deal of flexibility during development - this is true, but it will greatly increase the speed of your application. And speed is more important than the comfort of work of a developer, isn't it? If you want flexibility, try to use Photoshop (JSX) and Flash (JSFL) scripts to automate your tasks.“
[Last Modified By liuhuan, at 2007-12-07 14:26:35]
Comments Feed: http://www.liuhuan.com/blog/feed.asp?q=comment&id=809
There is no comment on this article.
You can't post comment on this article.