Programming Language Efficiency Ranked: An Old Standby Wins

You probably have a favorite programming language (or set of languages), but are they considered ‘green’? A new research paper shows faster languages don’t always use less electricity, while some old standbys are actually pretty efficient.

Whether a programming language is compiled, interpreted, or run on a virtual machine, a computer’s CPU is responsible for about 88 percent of a language’s power consumption, according to a team of six university researchers in Portugal. It’s worth noting that interpreted languages have the highest variation, sometimes causing the CPU to consume as much as 92.9 percent of the power – or as little as 81.57 percent.

Researchers also found peak usage of DRAM and energy consumption “is almost non-existent.”

It reached its conclusions by using the Computer Language Benchmarks Game, free software that runs a programming language through simple and complex algorithmic problems and framework tests. Researchers used 10 different models to test language efficiency. They tracked three different metrics from the 10 tests: energy used, time spent on the tests, and megabytes (Mb) used to complete those tests.

The C language ranked highest (i.e., most efficient) for energy and time, and fell to third on Mb usage. Rust was a close second on energy and time, but plummeted to seventh place for Mb used. C++ was third in the first two categories, dipping to fifth when researchers measured how many Mb it used.

Efficiency also depends on what task is being performed. Researchers note that, while C is overall quite efficient, a test involving the scanning of DNA databases for a unique genetic sequence proved Rust to be more efficient in that specific instance, while C was the third-most-efficient.

One issue researchers wanted to parse was whether or not a ‘faster’ language was more efficient. The common thinking here is that less time spent using resources will correlate to more efficiency, but that’s not always the case. Researchers plainly state: “A faster language is not always the most energy efficient.”

In conclusion, the researchers offer the following:

From our experiment it is clear that different programming paradigms and even languages within the same paradigm have a completely different impact on energy consumption, time, and memory. We also see interesting cases where the most energy efficient is not the fastest, and believe these results are useful for programmers. For a better comparison, we not only measured CPU energy consumption but also DRAM energy consumption. This allowed us to further understand the relationship between DRAM energy consumption and peak memory usage, while also understanding the behavior languages have in relation the energy usage derived from the CPU and DRAM. Addition- ally, the way we grouped the languages is how we felt is the most natural to compare languages (by programming paradigm, and how the language is executed).

We can say there are some interesting efficiency issues with some languages. Swift uses almost three times as much energy as C, and takes four times as long to compile – but is nonetheless lauded as a ‘fast’ language.

But you also wouldn’t write an iOS app in C. Developers and engineers should always use the language that works best for their particular use case. Efficiency is interesting and important, but even with programming languages, the idiom ‘horses for courses’ rears its head.

Related