Green Programming: Reducing your carbon emissions when coding

Martín González

2023-01-18 11:33:05
Reading Time: 3 minutes

Since the first digital computer was invented in 1945, many programming languages have come and gone while technology has rapidly evolved. It is estimated that as of today, around 700 programming languages exist. However, other estimates reach as high as 9,000 to ever exist (not that all of them were ever relevant). From very famous and old ones such as C, invented in 1985 and still the second most relevant programming language (according to the TIOBE Index) today, Python (who would’ve thought that it was invented in 1991?) to more recent ones such a Go (2007), which is gaining popularity every year. Though these languages have come and gone according their relevance within their concurrent technological context, we are now approaching an age where a language’s energy consumption must be considered alongside its other attributes.

Energy consumption in programming

One thing that usually doesn’t come to mind when starting a new coding project is how much energy it might consume when in a production setting. This can be measured through many metrics, though complexity of code can most directly contribute to reducing energy consumption (fewer loops, etc.). Even still, have you ever thought that the very programming language that you are using might be the problem and not the code itself?

A paper carried out by six Portuguese researchers from different universities presented a series of popular programming problems as performed in different languages, with a total of ten problems taken from the Computer Language Benchmarks Game. The normalised results for energy, time and memory consumption are shown in the table below.

Total
Language Energy Language Time
C 1.00 C 1.00
Rust 1.03 Rust 1.04
C++ 1.34 C++ 1.56
Java 1.98 Java 1.89
Pascal 2.14 Go 2.83
Lisp 2.27 Pascal 3.02
Swift 2.79 C# 3.14
C# 3.14 Lisp 3.40
Go 3.23 Swift 4.20
JavaScript 4.45 JavaScript 6.52
TypeScript 21.50 PHP 27.64
PHP 29.30 TypeScript 46.20
Ruby 69.91 Ruby 59.34
Python 75.88 Python 71.90

As you see, on average, to execute the same code, Python takes up as much as 76 times more energy than C! That is quite a lot, if you ask me, especially considering the fact that Python is right now the most popular programming language according to the TIOBE Index. But why is this? Why does such a popular language have such poor performance in energy and time? This is because, contrary to the top languages in the ranking, Python is an interpreted language instead of compiled. This means that to execute the different instructions that the program has, an intermediate program is needed to translate them, which ends up in a slower execution and more energy consumption.

So what if my code consumes more energy? It’s just electricity, right?

Well, not quite, thinking that electricity equals clean energy is a big mistake. According to Our World in Data, as of 2021, around 62% of electricity production worldwide came from non-renewable sources like oil, gas & coal (nuclear accounts for around a 10% of the total, though it has other downsides, like the residues).

Looking at the European Union, as of 2021, around 37% came from fossil fuels, and 25.52% from nuclear energy, making a total of a little bit less than 40% of electricity coming from renewable sources.


Your location when coding and deploying systems can also be important, since, as we’ve seen, the electricity that powers data centers across the world comes from different sources. Nowadays, many cloud providers let the user select the region, so bear that in mind next time you are deploying a service.

Green Programming in Aviation

As all other industries across the world, the aerospace industry relies on IT systems for almost all its operations. Without software, modern aviation just wouldn’t be possible. From flight plan managers and radars, to air traffic control systems and auto-pilot systems within the aircrafts, everything relies on code. However, most systems in this industry consist on legacy setups from up to 50 years ago…or even earlier! Most of these older setups run on proprietary monolithic structures that run on old code, which can be suboptimal by today’s standards. A next step in the industry towards green programming (and green computing in general) would be to modernise these systems to a quality worthy of the second decade of the 21st century.

© datascience.aero