avoid mickey mouse programming
Code tuning techniques
Code tuning techniquesCode tuning differs a lot from refactoring because it doesn’t always improve code readability, and changes that you make are not meant to improve the internal structure of a software.
If the changes performed in the code don’t degrade the code’s readability, we believe that you are refactoring rather than fine-tuning.
One of the practices most of the fine tuning papers recommend is to stop testing a condition when an answer is known; this means that it could be better to split a short circuit evaluation into two separate evaluations, but this is not true in the Flash Player, if you make a simple test like this
private function testShortCircuit():void{
var start:int;
start = getTimer();
for(var i:int = 0; i < 100000000; i++){
if(i > 3000000 && i < 4000000){
break;
}
}
trace("Short circuit", (getTimer() - start))
start = getTimer();
for(var j:int = 0; j < 100000000; j++){
if(j > 3000000){
if(j < 4000000){
break;
}
}
}
trace("Not short circuit", (getTimer() - start));
you’ll get a result that clearly shows that short circuit in the Flash Player is faster
Short circuit 307
Not short circuit 331
Arrange tests in the conditional statements by frequency, putting the most common case as the first condition to evaluate is a good strategy for increasing performance.
We strongly recommend grouping together loops that operate on the same set of elements, so that you can remove a loop and acquire a lot of speed processing.
When dealing with nested loops we recommend always putting the busiest loop in the inside and moving the one with fewest iterations to the outside.
There are many common mathematical operations that can be done faster than usual
Operation Faster solution
Math.floor(1.5); int(1.5); Math.ceil(1.5); int(1.5) + 1; Math.abs(value); var test:Number = value < 0 ? value * -1 : value; value / 2; value >> 1 value * 2; value << 1
If you need some values multiple times in a component / class and these values need to be calculated we suggest that you calculate them only once and put the in constants for faster access
private const SOME_VALUE:Number = Math.sqrt(Math.pow(Math.PI, 10));
If the calculation varies during the execution of the class we suggest putting it in a method and avoiding having multiple pieces of code for the same calculation duplicated in the code.
Other resources
| Print article | This entry was posted by Giorgio Natili on October 4, 2009 at 2:51 pm, and is filed under code. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 7 months ago
it’s worth to say that the bit operations (like value >> 1) are meant to work on integers and return s only integes.
So if you try to (3 >> 1) you will get 1 as a result.
Also they do a “awful” (just because it can be unexpected) job at rounding for negative numbers. (-3 >> 1 == 2).
For the same reason using int(-1.5) in place of the floor() method will give you wrong results.
In my opinion, you should be really confident about what kind of data you are working with and what kind of results you need before switching to bit operations.
about 6 months ago
Hi Alessandro,
I agree with you and thanks for the comment, I believe it can help a lot this small blog…