Home > Technical, The Future > How green is your code?

How green is your code?

January 20th, 2009

Is your code fast and efficient? Is your nasty for-loop killing penguins somewhere and You don’t even know about it? We all hacked and brute-forced through a problem at one time or another – it is inevitable. How much CO2 will your hack release into the atmosphere? Writing power-inefficient code is like driving a fuel-inefficient car – we’ve all done it but it’s just not cool anymore. With green finally becoming the buzz-word everywhere, it is time to take a look at a few numbers surrounding the environmental cost of bad code.

The issue at hand is the amount of power (as in electricity) that your code will consume when running on a large number of users’ computers (since you write awesomely popular software) or over a long period of time (if your software will be in use for years).

Hacks. Free(-as-in-beer)?

Suppose you hacked together a quick Flash widget to go on the front page of your up-and-coming website. Your code is nasty and the widget takes up almost all CPU power on a single core but you have no time to fix it and release it as-is.

Lets run some very basic numbers. A normal user might spend 10 seconds viewing your front page before navigating to another page without the widget. Lets also say that a typical Intel Core 2 CPU will consume about 33W when operating one of its cores at 100% load because of your crappy code. For those 10 seconds on the site, your widget has consumed 33W x 10s / 3600 s/h = 0.092 kW·h.

Let us now assume that you site got mildly popular and received a million visitors over the course of a year, each of which spent only 10 seconds on pages containing the widget. Now your widget has consumed about 92,000 kW·h, or 92 MW·h.

Garbage In – Carbon Out

The average household in the United States uses about 11,000 kW·h (11 MW·h) of electricity, each year. Congratulations! Your inefficient code has used up enough electricity to power almost 8 homes for a full year!

If you live in the United States, half of this electricity is likely to come from coal. Coal has en energy density of about 2.0 kg / kW·h meaning that the amount of coal burned to power your inefficient code is 0.5 * 92,000 kW·h / (2.0 kg / kW·h) = 92,000 kg = 92t. Yes, that is 92 tons of coal!

Given that producing energy from coal also releases CO2 at a rate of about 0.950 kg / (kW·h), your code will have resulted in 87.4 tons of CO2.

These numbers seem quite depressing. What if your widget ends up on the front page of a big site with tens or hundreds of millions of visitors a year? How much environmental destruction can one nested for loop cause? How many dead penguins will you be responsible for? Gasp!

Save The Planet?

But let’s be realistic, code will always consume power and you can only do so much to reduce that. Some applications will have a net benefit of reducing energy consumption by making people more productive and efficient with their time and energy (your punch-the-monkey widget might or might not be one of those).

The point of the matter is that, even when programming, one can consider the environment. If that consideration provides you with the impetus to sit down and properly re-write a power-hungry piece of code then you have made a real physical change in the world. And now, you can put a number on it.

Safe the CPU cycles – save the world. :-)

Technical, The Future ,

  1. No comments yet.
  1. No trackbacks yet.