views:

3243

answers:

29

Are there any tools and techniques to build more energy efficient applications? Any articles, rules or best practices for green programming?

+5  A: 

Interesting question - the first thought that occurs to me is going lighter on CPU is more energy efficient (as the CPU runs cooler and fans don't need to come on - plus on some OSes an idle CPU goes into a more energy efficient state). So eliminating things like busy waits for example, and optimising very CPU intensive algorithms may help.

Similarly anything that can be done to wring more life out of an old server may be more energy efficient in the 'green' sense (not having to build and ship and package a new server, not needing to dispose of the old one, etc).

(I always thought the climate simulations running on grid computers were a bit ironic - I ran one for a while on my Mac and it always made the CPU temp go up, the fans come on, and the power supply run hotter - to say nothing of the CO2 emissions from all that :-)

frankodwyer
+4  A: 

Recently, while preparing a seminar on Design Patterns, I came across an article (can't find it now) that looked at design patterns and, specifically, the Bridge pattern IIRC, in the context of energy efficiency in portable devices. I do not think, however, that many people take this seriously, since we live in a world where to get a more energy-efficient hardware platform, you just wait for one.

If we are talking about battery life and such, then obviously any sort of optimization which makes us use less instructions is better. Also, doing things in RAM instead of swapping to HDD might be more energy-efficient.

Dmitri Nesteruk
+17  A: 

Borland C++ Builder 5 and 6 had a horrendous inefficiency for many many years that cause compilation to run about 10 times slower than necessary.

A rough estimate of number of programmers (20,000), minutes/day of wasted CPU time (15), PC power consumption (150 W) and lifespan of the bug (5 years) gave me a figure of about 93 Megawatt hours, or 93 tons of CO2

All from one tiny bug...

So, not really an answer, but I think your question is a highly valid one!

Roddy
+19  A: 

I'm going to answer your question with my own: Is there enough benefit for the cost of green programming?

My knee jerk reaction is that green programming would violate the 80-20 rule, and we could be getting much better green results elsewhere (such as switching to a processor that draws 20 W instead of 47 W)

Good question nonetheless!

Gavin Miller
But multiply that with the number of users of a program, and consider the total cost of switching processors (which for laptops would mostly mean dumping the old one and buy (-> produce) a new one). Regarding often-used programs I'd say that knee-jerk reaction is totally wrong.
mghie
The answer is of course yes. Anything running on a grid or cloud for 24 hrs a day is going to use an awful lot of power. The good thing is, making an app faster often also makes it more energy-efficient
Stephan Eggermont
One drop of water in the ocean in nothing, but if we see by the ocean perspective (One drop x "n")... that will change the way we look at the question ;)
balexandre
A: 

I've not come across much at an application level but at the db level there are a class of databases called sensor networks. These are typically battery powered and in remote locations so power consumption is an important factor.

Sensor dbs are usually in a snooze mode and 'wake' when they need to collect data or when they transmit. Without reading the paper about them, this stuff seems very 'embedded' and I've no idea on metrics but I would guess power consumption would be higher when :

  1. thrashing the hard drive
  2. doing heavy mathematical calculations
John Nolan
+4  A: 

At application level, many times caching is overlooked: if you already have a fresh result, you don't have to recompute it. Less computation -> lower power consumption; also, usually less data transferred (-> lower power consumption - infinitesimal, but it adds up). Of course, caching logic has to be lighter than the computation itself, otherwise it defeats the purpose.

For example, in HTTP this can be done with conditional queries - yet rarely do you see it (it can be tricky to keep track of response freshness).

Piskvor
+4  A: 

The Less Watts site is a good place to start.

Ant P.
A very good link. It also does list many of the things that the answers to this question here contain. Some do however make most sense when implemented on the OS level. It's up to the dev to make use of them, however. Like to use everything Windows provides to eliminate polling in your application.
mghie
+3  A: 

At the IPCV 2008 conference, I saw a presentation that included data for calculating the cost of each instruction on a mobile device in units of power (I believe it was picowatts). Given that as a starting point, I imagine you could make an optimizer that reduced cost simultaneously in computation and power. I don't think it's trivial, but it is certainly possible.

plinth
+2  A: 

Eliminate the servers, P2P rules :)

  1. More efficient use of available machines. They have enough storage and networking capacity that it should be simple to get rid of the big iron.

  2. Make sure the backlight of the monitor is out when the machine is not used. Spin down the disk. Not really at application level.

  3. If you take a look at current architectures, processing is happening too far away from memory. Going off-chip takes an awful lot of power. If you just count chips in your PC, you'll notice there are more memory chips (8 or 16) than processing ones (CPU+GPU). It makes sense to move the processing towards the memory, but that means a non-unified memory model, which is difficult to program. Basically, a grid inside your PC.

Stephan Eggermont
A: 

Aside from the absurd solution where you just don't compute (and therefore you don't need the electricity...)

The aim is simply to do the most work with the least amount of energy. Outside of the hardware realm (which you just lower the voltage and frequency), you just have to make sure the work could be done in the least amount of actual execution time.

This is probably a pain to measure because of OoO Execution, etc etc.

I wouldn't lose sleep over it, as long as you use efficient algorithms and such, and not bloat your code unnecessarily.

Calyth
+10  A: 

One generalized answer to the question is don't poll for changes or periodically scan for something to do. Use operating system notifications to process information on an interrupt driven basis.

When you do process information do it efficiently and as a 'bulk' endeavour. One big write to HDD is better than several smaller writes as the disk system can switch to lower power states sooner. The same applies to use of CPUs, hardware radios..etc.

While virtually meaningless on most hardware connected to the grid.. platforms such as laptops and especially mobile phones can benefit quite a bit by applications designed to for power effeciency (Use the CPU and system resources as sparingly as possible)

Einstein
A: 

An interesting thing to think about - how CO2 do you produce while optimizing your code, and how much CO2 is it going to reduce? I can imagine that developing software is a quite CO2-intensive process... ;)

Vilx-
+73  A: 

Wow, the possibilities are endless here:

General Ideas:

  • Compile much less frequently. Compiling is CPU intensive, and CPU's are watt hogs
  • Stop running those crazy unit tests all the time.
  • Heck, shut down that Continuous Integration server! Come on, you really only need to build every couple of days anyway, bottom line.
  • Limit checkins to your source control system. Think of all those watts burning down the wire and on the remote server!
  • Stop commenting your code. (Okay, so that should probably be "Don't start commenting your code".....)
  • Save on Pixels. Set your screen resolution to 640x480. Fewer pixels, fewer watts!
  • Code only in APL.
  • Don't code any more bugs. Bugs require more compiles, more testing, more typing, and more CPU time.
  • If your system allows code folding, keep as much code folded as possible. Saves on monitor output.
  • Keep all your caffeinated beverages at room temperature.
  • Unplug your USB Missile Shooter. Totally forget about the humping dog Memory stick.
  • Take the batteries out of that Light Saber behind your desk and use it without the soothing purple glow.
  • Generally, keep all your numbers small. I'd suggest never using big numbers. Big numbers probably use more energy.
  • Get a mouse-ball based mouse. That red light has to be burning watts.

Since keystrokes require energy to process and display:

  • Many languages let you put multiple statements on one line. Do that as much as possible.
  • Pursuant to that, never break long lines of code into multiple lines.
  • Don't refactor. Too often this creates more code that you have to scroll through.
  • Name all variables with as few letters as possible. Preferably one letter. Generally two. Three max.
  • Stop putting blank lines in between lines of code. Saves on Enter keystrokes and scrolling!
  • Shoot, eliminate unnecessary white space altogether. When you get right down to it, what exactly does white space do? Nothing, that's what.

That's all I can think of right now. There has to be more stuff like this! Come on guys, let's put our heads together and really go green!

Nick Hodges
:o) Ball mouse? No mouse is even more efficient!Also, remember to code FAST. Spending too much time thinking instead of typing code wastes costly cpu cycles.Finally, if you already know what you're typing, you can leave the monitor off until you need really to check what you've typed.
Wouter van Nifterick
up vote for hilariousness
Carlos Rendon
Turning off the monitor while you are typing! Brilliant!
Nick Hodges
A lot more up votes after your blog post. ;-)
Jim McKeeth
And remember to always use hybrid-electric computers instead of those gas-guzzling internal combustion PCs.
Angus Glashier
When you HAVE to code in the editor, use the old classic color set. The white background has to be using more energy to illuminate ALL of those whitespace pixels. Heck, better to set it to green on black for a real retro energy saving feel.
skamradt
Actually use your white board again for modeling and collaboration.
skamradt
Don't forget to write and debug your code on paper with a pencil first while your computer is off. Then all you have to do is type and compile!
Rob Allen
I think he hasn't asked how to "develop" in a greener way, but how to develop "greener" applications, so they consume less energy when running without decreasing functionality.
Fernando Miguélez
Get everyone to agree to use only one software for programming, and that software needs to start with the letter "D", end with the letter "I" and contain 6 letters.
lkessler
Dim the monitor and works without lights?...man your answer is excellent +2
Saif Khan
Stop using computers altogether? I think we missed the point here, using technology rather than doing things the way we used to is environmentally friendly in and of itself.
Chris Ballance
Excellent illustration of the absurdity of the extreme Enviornmentalist wacko alarmists.
moffdub
Thank you, my non eco friendly monitor is now covered in second hand beer. +1
Tim Post
There's no indication Jlouro is an "alarmist." He asked a simple question about how to consider energy efficiency in development. He never said that was his only concern, or even the top one. Moreover, energy efficiency is as much about saving money as the environment. It's stupid that the accepted answer to a legitimate question is a joke, and not even marked community wiki.
Matthew Flaschen
+8  A: 

Energy efficiency depends on the hardware and the OS that you are running on, as well as the library, that you are using.

If you are doing application development on a typical PC, then your application will be waiting for system messages most of the time. Typical frameworks (e.g. C#, Java, Delphi, Visual Basic) contain the basic functionality for these messaged to trigger events that your program will react to (e.g. OnClick). If you stick to this way of programming, there is not much that you need to think about. When your application is waiting for system messages, the OS knows, that the application is not busy and can make use of its built in power saving functionality. Things that you might consider are improvements on the IO-part of your program (e.g. combining several file reads or writes into one combined one).

If your application involve a lot of processing (e.g. when you need to create independent threads to handle the processing without freezing the UI) you will need to think more about power efficiency. Optimizing the IO-part may be a good starting point.

Things that you should generally avoid are busy loops polling for an event (e.g. change of a bit in a register, or the availability of data from another thread). Use synchronization functions of your OS (e.g. Events, Semaphores, Mutexes, WaitForSingleObject, ...) instead.

In embedded systems there are many more things to consider ( / under your control).

I am working at the moment on a wireless sensor network, where each node will be able to run on small batteries for years. In order to achieve that, two things had to be done.

1) Design a very power efficient hardware by selecting low power consumption parts with efficient sleep modes. Many Microcontrollers have the possibility to switch off sub systems when not in use. They also have various power reduction or sleep modes. During a sleep mode program execution is stopped, while all RAM is kept. Depending on the specific mode certain sub systems are still alive and will wake up the controller when needed.

2) Program the firmware to make use of low power modes, whenever possible. Switch off sensors / transmitters when not in use. Let the controller sleep as often as possible (e.g. use timers to wake up from programmed delays instead of busy wait delays, when waiting for external events program a wakeup interrupt and go to sleep). As a result of these endeavours your firmware becomes much more complex. There are many more states, that your program maybe in, and error conditions (e.g. timeouts) need to be cought by making use of subsystems.

Jan Mitrovics
+11  A: 

Intel have some Energy Efficient Software Guidelines on their site, and links to a wide range of tools for helping with this.

Looks like a good place to start!

Roddy
A: 

Use a native code compiler. Yes, you will have to write code to explicitly free used objects. The benefit is that you save the run time environment from having to run a garbage collector that continuously tries to determine which objects aren't in use anymore.

Also, disable compiler options that waste run-time CPU cycles, like range checking. A well tested program must never violate range constraints, so range checking is only needed during debugging.

+2  A: 

Energy efficiency in programming is, when the value is maximized and the environmental costs are minimized. In other words, it can be green programming, if your application consumes more computer resources but has a shorter time to market. Simply because the environmental costs of a late product launch can be much bigger.

The simplest way to do green programming is to eat less pizzas and drink less cola. Buy local food, instead.

Lars D
A: 

During test phase for different operating systems, different configurations, use virtualization (virtual machine) instead of adding new computers or servers. Virtualization can also be an alternative when we need to add more servers while the usage of available servers are not optimal (underload) yet.

bprasetio
+3  A: 

Of course you probably don't want to purposefully build software that is energy-ineffecient, but I think this really misses the point.

Productivity software in general should be considered energy-efficient, because no matter how much energy the program itself requires it's going to be orders of magnitude less than the real-world task it replaces. So what if your app requires an extra watt to retrieve and display that fancy document from the file server, if it means a person didn't have to get up and retrieve and actual real paper document from a filing cabinet. Now there's no energy expended in producing and delivering the paper (or the filing cabinet and office space to hold them both, for that matter).

Even eye-candy UI features can be energy efficient. Maybe your new flashy/pretty whizbang UI feature requires more cpu time, but if it helps someone complete a task in less time than it used to you'll still end up with a net savings. Or maybe it helped drive sales of the software in the first place, such that without the nice graphics people would be using inferior software and therefore less productive.

Using StackOverflow as an example, let's say it was built to be very energy inefficient, such that even simple searches require 4 times as much electricity as a comparable service. But if it saves you from spending an extra hour Googling those other sites it comes out to a net gain.

In other words, if you believe in you have a quality product, then you have an energy efficient product. It's much more important to focus on providing a good workflow that people will actually use.

Joel Coehoorn
A: 

Set up your IDE to show a black background rather than white. This will give you extra battery time on a laptop. OS X users can actually invert the entire OS's colours (black <-> white) by pressing CTRL+ALT+SHIFT+COMMAND+SPACE+BACKSPACE+INSERT+8 (or something like that).

too much php
That isn't true anymore (it used to be). Now the big driver for screen power consumption is the backlight behind the display itself. That runs at full regardless of what color the pixel is. Black may even use more juice, since you need more physical particles in the pixel to fully obscure it.
Joel Coehoorn
A: 

Develop on VIM with a black background. Build your apps to have dark backgrounds.

Ubersoldat
See my comment to the previous answer: black isn't more power efficient any more with modern displays.
Joel Coehoorn
+1  A: 

Support older operating systems and hardware, some hardware upgrades are driven by new software.

Tony Edgecombe
+4  A: 

Work from home. I did a quick estimate of kilos of CO2 put into the atmosphere by my commute, and it's frightening.

David Thornley
A: 
lkessler
+5  A: 

There is an excellent PDC video here on the topic wrt Windows:

http://channel9.msdn.com/pdc2008/PC19/

"Inefficient background activity has a dramatic impact on system performance, power consumption, responsiveness, and memory footprint. This session demonstrates best practices for background process design and dives deep on the capabilities of the Service Control Manager (SCM) and Task Scheduler."

Scott Hanselman
A: 

Don't open your computer ;)

If you need to open your computer, don't send spam mail :O

If you need to send an email, you must be use plain text. Don't use the HTML format and pictures.

Don't browse rich web site :P (Like as this site.)

Joke, joke...

A: 

As for energetic efficiency, from now on all my software (tools to) will put the monitor on standby if the software or the computer is not used for x minutes.

Of course there many tools and system way to do this, but not every user know about it or know how to do it. So from now on it’s a standard feature.

Saving the planet one disconnected monitor at a time.

Jlouro
I'm not sure i'd do this... users might have a good reason to keep it on -- after all, it is a *user* setting isn't it?
RCIX
+1  A: 

I think something that is going to help here is the advent of cloud computing.

Now that you are going to be billed for every CPU second, every HTTP request, every byte of storage, there is much more motivation to code economically.

Thilo