Click here to preview the new Fast Company

Want to try out the new

If you’d like to return to the previous design, click the yellow button on the lower left corner.

Can Computer Code Be Greener? Facebook Thinks So

One of Facebook's green initiatives involves converting their software from one language to another. What makes one code greener than another?

Facebook, which is getting some negative PR on environmental issues, recently announced several green initiatives. Some of them are intuitive: getting employees to cut down on water usage, a shuttle bus, composting. Most intriguing, though, is one initiative to ensure that the very code written by Facebook's programmers is itself as green as possible, something achieved by converting Facebook's code from one language called PHP to another called C++.

How can it be that one type of computer code is "greener" than another? And what is the greenest code?

Some programming languages are more energy efficient than others because they simply run faster. A program that can be executed faster reduces the load on a central processing unit, which in turn demands less energy from the massive data centers run by Facebook that pull electricity from the grid. The question, "Which programming languages are greenest?" is actually just another way of asking which produces the fastest code, says David Andersen, an assistant professor of computer science at Carnegie Mellon. "That's a question we computer geeks love to fight about."

For the most part, though, the computer geek consensus is that the so-called "interpreted" languages like PHP are slower than the "compiled" languages like C++. In general, interpreted languages are easier to program in, but take longer to run. "If you write the same program in PHP or C++ ," says Andersen, "you'll find the C++ version takes ten times as many lines of code to write, making it harder, but the program in C++ runs five times faster."

It might seem counterintuitive that a program with more lines of code would run faster. But since C++ is rawer, in a sense—closer to those ones and zeroes that computers ultimately run on—it means there is less digestion, so to speak, involved. "C++ is converted to machine code before being run," says Joel Ross, a Ph.D. student in UC Irvine's Social Code Group, "whereas PHP, every time you run it, it has to get converted."

Is C++ always the fastest language? Not necessarily, says Ross: "Languages can have different strengths." (And there are hundreds, if not thousands, of them.) Kathleen Fiehrer, an Intel employee affiliated with the Climate Savers Computing Initiative, explains that in a personal computer, you want a language that will "race to idle"—that is, finish a given job as fast as possible, then let the computer rest. For servers, on the other hand, which are constantly active, you just want to be assured that you are getting an optimal performance/watt ratio.

So is there such a thing as the "greenest code"? No. "In general, I would say there is no one language that is the fastest—it's application specific," says Bill Tomlinson, Director of the Social Code Group and author of the book Greening Through IT.

Tomlinson further complicates matters by questioning the notion that an energy-efficient Facebook necessarily translates into a greener—in the sense of more sustainable—world. If Facebook halves its energy expenditure but doubles its size or its user base, "that may not make the world more sustainable, but rather just make more Facebook," he says.

[Image: Flickr user n3wjack]

Add New Comment


  • Leo Toribiio

    While it is generally true that programs written in C++, Pascal and many other compiled languages run faster than programs written in interpretive languages, it is not necessarily always the case. I have actually seen programs written in assembly language and compiled languages that execute more slowly than programs written in Basic, PHP, and other interpretive languages took to run the same functions. There have always been programmers plagued with tortuous logic, who, if they were busdrivers, would find their way from New York City to Washington, D.C., via San Antonio, TX or even Los Angeles!

    Leo Toribio
    Pittsburgh, PA

  • Russell

    As a programmer, it's really not that ridiculous. The huge data centers Facebook uses require enormous amounts of energy, and making the website run faster, even by microseconds (which is a lot less than this change can provide) adds up, especially considering how many users Facebook services. The energy costs could be several times smaller.

  • Michael Brown

    "Can Computer Code Be Greener?"


    Can this consideration be more pointless. You've got to be kidding me. We are now slimming down even our computer code in an effort to curb energy use? And all for the cause of stopping something we didn't start?

    How many micro seconds more does this PHP really take to run a program than C++? There comes a point where we are just spliting hairs with all of this inane carbon footprint reduction, and this has got to be it.