Java is one of the most energy-efficient languages, Python among least energy efficient
Energy efficiency isn’t just a hardware problem. Your programming language choices can have serious effects on the efficiency of your energy consumption. We dive deep into what makes a programming language energy efficient.
For years, we’ve conflated performance with processing time. “How well does it do?” meant “how fast is it?” But these days, things have changed. Developers, engineers, and even consumers will do a lot to avoid a heavy drain on the CPU in order to preserve their batteries. Energy efficiency started on the hardware side, but now it’s trickled down into software development.
There are a lot of questions that arise when you try to measure the efficiency of a programming language. For one, what sort of metric are you using? Is a faster program language a more energy efficient one? Is a faster programming language a greener one?
However, comparing languages is difficult. The performance of a language can be easily improved just by the quality of its compiler or virtual machine. Improved source code is as crucial as optimized libraries.
A team of Portuguese researchers studied 27 of the most popular programming languages to see if there was any relationship between speed and efficiency.
Using the Computer Benchmarks Game, the team of researchers tested these languages by compiling/executing such programs using the state-of-the-art compilers, virtual machines, interpreters, and libraries. They then analyzed the performance of the different implementation considering three variables: execution time, memory consumption and energy consumption.
What Pereira et. al. found wasn’t entirely surprising: speed does not always equate energy efficiency. Compiled languages like C, C++, Rust, and Ada ranked as some of the most energy efficient languages out there.
However, Java is one of the fastest and most energy-efficient object-oriented language. Interpreted languages like Perl, Python, and Ruby were among the least energy efficient. As the researchers discovered, the CPU-based energy consumption always represents the majority of the energy consumed.
On average, if sorted by their programming paradigm, the imperative languages needed the least amount of memory, followed by the object-oriented, the functional, and finally the scripting languages.
So, is it possible to choose a programming language based on energy, time, and memory usage? Well, yes. C is the clear winner across all the fields. But if you’re not interested in coding in C, there are some interesting options.
Go and Pascal do rather poorly in the straight efficiency test, but they do better if you’re considering languages based on time and memory or energy and memory. For those interested in improved energy and memory, Rust and FORTRAN are also decent options.
Test how efficient your code really is
The researchers have an awesome thing up over at GitHub where they’ll evaluate your code to see how energy efficient it is. It’s only three simple steps.
- Create a folder with the name of you benchmark, such as
test-benchmark, inside the language you implemented it.
- Follow the instructions presented in the Operations section, and fill the
- Use the
compile_all.pyscriptto compile, run, and/or measure what you want! Or run it yourself using the make command.
What do you think? Will this change your programming language choices? Let us know in the comments below!