Optimized code could help reduce global warming
October 15th, 2007 . by polyGeek |
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:
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:
var someDecimalNumber:Number = 3.14159265
var someNumber:int = int( someDecimalNumber );
And my favorite is instead of dividing you should multiply by a decimal as such:
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:
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.







You’ve got a slight typo in the shifting part. To multiply by two, you left-shift only one place.
var twice:Number = 123 << 2;
Not sure of the performance effects though.
[Fixed thank you for noticing.]
@George, thanks for that. It’s all fixy now.
before? In heart and spirit of Environmental protection, Polygeek wrote great article about how optimized code could help reduce global warming. …
Hooray for Dan! Did you see that Greenpeace’s blog mentioned you as the most intriguing headline :-)
http://weblog.greenpeace.org/makingwaves/archives/2007/10/happy_blog_action_day.html#more
@Collis, That is so cool. Thanks for getting me on this wave!
????? ?? ???????? ???? ???????? ????? ???????? ????? ????????? ?? ??????? ???? ?? ????? ???????? ?????? ??? ???? ???????, ????? ?? ?????? ???? ?????, ???? ? ????? ????? ????????? ??????????. ??? ??????? ?? ?????, ?????? ? ??????? ? ??????????? ???????????, ?? ?????? ????? ?? ???-??????? ?????????? ? how-to ?????????? ?? ?????? - ???????? ??? ?? ?? ???????? “?????” ???????? ??? ??? ????????????? ????? ???? ???????????? ?? ???? ?? ?…. …
The premise of this post is ridiculous. Even stipulating for the sake of argument that cyclical “global warming” is man-made (it isn’t to any significant degree) and that CO2 is the cause (it isn’t as CO2 volume in the atmosphere has always lagged warming cycles in the past) the amount of any energy actually saved in machine cycles would get more than burned in one phone call or e-mail message to product support to deal with a bug caused by code cuteness. The first comment identified a bug even in your trivial example!
If we all just live our lives worried about the important stuff (like doing a good job at work, supporting our families, etc.) and being ever vigilent to protect our liberties (i.e. offset the damage that liberal organizations like Greenpeace and the Sierra Club do) the Earth will be just fine.
Right Hook
BootsOn Blog
@RightHook, you caught me. My plan all along was to subtly increase the amount of energy burned and therefore accelerate the rate of global warming. This is all part of my global domination plans which revolve around buying up cheap inland property and selling later after the coasts have flooded.
I guess I’ll have to come up with yet another plan to conquer the world. Thanks for that!
My latest scheme is to try and find a way to convert sarcasm into CO2.
<p>@no regrets, err???? My Bulgarian is a little rusty but I think you said nice things. In your post that links here to polyGeek.com there’s a link to the movie Lethal Weapon at IMDB. Maybe you were commenting on how I have Mel Gibson-like good looks. If so then thank you very much. :-)</p>
interesting way to include the topic of the environment is at polyGeek.com, Optimized code could help reduce global warming. Clever, these …
Y ou have a great sense of humor.
Optimized code could help reduce global warming …
A good C++ compiler will optimize every one of these examples for you. (Well, probably not the Math.Floor one). It’s too bad AS 3 doesn’t do this yet. :(
@mb, Oh yeah! Well my compiler can beat up your compiler. :-)
Seriously, I dunno. Maybe it’s a factor of Actionscript being much newer than C++ that the compiler isn’t as streamlined. But that’s cool that C++ works that way. Hopefully that will trickle into Actionscript soon.
toward the environment continues to create problems. For instance consider this selection of posts for blog action day. Following the usual formula for personal moral advice these posts tell people …
bitwise is not the same as dividing or multiplying by two … on Numbers. That will convert the number to int and divide by two. For example, doing those operations on the value 5 will not yield 2.5.
You should probably say “dividing or multiplying integers by multiples of 2 are faster with bitwise”
@Danny Miller, thanks for pointing that out. I added a note to that effect.
Leave a Reply
Dan Florio - aka polyGeek - is a freelance Actionscript Flex/Flash/AIR developer living in Crestline, California with his wife Jill.
Check out his portfolio for more information about his skills and availability.
Poll
Other Sites by polyGeek
Topics:
Download Flex3 for a
free 60 day trial
follow polyGeek at http://twitter.com
© Copyright 2008 polyGeek.com / Dan Florio, All Rights Reserved Except Where Explicitly Stated
M2 Websites