Optimized code could help reduce global warming
|
Everyone wants to optimize their code so that it will perform faster and give the user a smoother more entertaining experience with their RIAs and cool 3D sites created with Papervison3D. But have you ever stopped to think that by its very nature optimized code uses fewer CPU cycles and therefore less energy. In doing so you reduce the amount of carbon dioxide in the atmosphere.
So here are a few tips to optimize your code and save a few icebergs in the process.
When iterating through an array you should declare the length of the array outside of the for-loop as such:
1 2 | var len:int = someArray.length; for( var i:int = 0; i < len; i++ ) {... |
You can apply that to AS2 by declaring len as type Number.
In AS3 never use Math.floor. It’s slow as frak. Instead cast the number as a type int as so:
1 2 | var someDecimalNumber:Number = 3.14159265 var someNumber:int = int( someDecimalNumber ); |
1 |
And my favorite is instead of dividing you should multiply by a decimal as such:
1 2 | var halfWay1:Number = ( endX - startX ) / 2; // slow var halfWay2:Number = ( endX - startX ) * 0.5 // fast |
Or if you really want to burn rubber then when multiplying or dividing by 2 you can use the binary right or left shift operator. As such:
1 2 | var half:Number = 123 >> 1; // same as dividing by two var twice:Number = 123 << 1; // same as multiplying by two |
Note: using right-shift to divide outputs an integer so it only works accurately with even numbers. Example 7 >> 1 = 3.
There are lots more great ideas here at OSflash.
Now go out there and write optimized code. If not for your users then think of the polar bears.






