Which Is One of The Most Wasteful Energy Industries in The World … Software!

Meet the most efficient programming languages: C, C++ and Rust

Photo by Nick Fewings on Unsplash

Which Is One of The Most Wasteful Industries for Energy In The World … Software!

Meet the most efficient programming languages: C, C++ and Rust

We all have a part to play in reducing our impact on our beautiful planet, but we are often so wasteful of its precious resources. For this, I think of the Apache blessing:

May the sun bring you new energy by day, may the moon softly restore you by night, may the rain wash away your worries, may the breeze blow new strength into your being, may you walk gently through the world and know it’s beauty all the days of your life.

Tech consumes energy like little else …

The debate about the Proof of Work (PoW) method used by Bitcoin and Ethereum is a hot topic just now, and some parts of the world are even trying to ban it for its energy consumption. Overall, proof of work would have seemed so sensible for Satoshi, but he/she could not have foreseen the rise of GPU factories, and where the price of Bitcoin has just made it so attractive to mine the cryptocurrency. At the current time, it is estimated to be consuming at least 60 TeraWatt hours of energy per year:

Figure 1: here

Along with this, the rise of data centres has seen them consume an ever-increasing amount of energy. In fact, they currently consume around 200 TeraWatt hours of energy per year, and account for around 1% of the global energy consumption:

Figure 2: here

And, so, as most industries have generally tried to make their supply chains, producets and services more energy efficient; in software, we just keep turning out computer programs that consume energy, and have little care for their energy footprint. Luckily, there are some companies who are taking this energy consumption seriously, and where AWS aim to have all of their data centres powered with renewable energy by 2025, and who have even been assessing the energy footprint of their software.

Every little bit of time saved in running a program, or in disabling unnessary services, will generally reduce our energy consumption. And, it may surprise you, but Python consumes so much more energy than C. So let’s have a look at some research on this, and see if we can find a programming languages that could cut down on the energy footprint of software.

CPU utilization and energy consumption

Every tick of a computer clock consumes energy. The more that a computer (when a physical device or running in the Cloud) has to do, the more likely it consumes more energy. The more energy we use, the more energy we need to cool the processors.

But, for most of us, it is an invisible cost. We don’t pay Amazon’s AWS electricity bill, so we problably don’t care that much. So, we want things to be done on our computers, and it doesn’t matter too much how much energy we consume. And, its often so wasteful. We create algorithms and methods that care little about the energy they are using, and the focus tends to be how fast our programs run. What’s the point having complex encryption methods which takes trillions of years to crack, if they consume so much more energy than one which still protects the user for their whole lifetime?

Overall, it is CPUs that consumes most of the energy in a computer and they are responsible for over 80% of the overall consumption of a computer. In 2017, Pereira et al [1] did an extensive survey of energy consumption with programming languages:

Figure 3 [1]

They found that C, C++ and Rust were the fastest and least energy-consuming languages, while the interpreted languages of Python, Ruby, Perl and Swift were the least efficient:

Figure 4: [1]

A surprising finding was that Golang did not perform well, even though it is a compiled language, and actually consumed more energy in the tests than the framework languages of Java and C#. This rang a few alarm bells in the industry, and that Golang was generally a relatively heavy consumer of energy, and perhaps was not the fastest in executing. For a few major companies, it has perhaps tipped them to move toward Rust rather than Golang in building their software foundation.

In order to understand this consumption, Couto et al studied the energy consumption of a range of programming language for their energy drain on systems [here][2]:

Figure 5: [2]

For this, the research team picked some standard tasks which varied in their computation:

Figure 5 [2]

While the number of programming languages is rather limited in this study, there are some strong conclusions. Figure 2 shows that C was the best in terms of both execution time and in energy consumption, while Java consumed 68% more energy, and was 65% slower. Perl and Ruby come in last place, with relatively large amounts of energy used against the most efficient ones. Perl, for example, used up over 84 times more energy that C:

Figure 6 [2]

One surprising thing here, again, is that Go would be expected to run faster and use less energy than C# and Java. But it can be see in these tests that it is actually slower and consumes more energy. Again, it can be seen that C is by far the most efficient, and the fastest in operation. Generally, though, it is the interpreted language — such as Lua, Perl and Ruby — that are the slowest, and the least efficient for energy consumption. In fact, in the test, overall, the Perl programs consumed over 84 times the energy than a C program.

Ranking of programming languages

The work of Pereira et al has since led to the development of a methodology for ranking programming languages for their energy impact [3]:

Within the paper (published in 2021), they classify the different languages in terms of being functional, imperative, object-oriented and scripted:

And so while the previous research has just measured CPU energy consumption, the new work looked into the more general energy footprint of each language for a range of tests:

Again, it was C, C++, and Rust that come out top, with C# and Java having middling performance, but where Golang does very poorly and ends up consuming more energy than JavaScript. As expected, there is a strong correlation between the time it takes to run a task and its energy consumption, but weaker in terms of memory usage:

Again, C, C++ and Rust come out well with memory energy consumption, and with the interpreted langauges (Ruby, Perl and Lua) being particularly poor in this respect:

For assessments, we can see that generally C, C++ and Rust do well in most tests:

In this case we can see that for the N-queens algorithm, that C consumed 0.05 Joules, while Python consumed over 111 Joules (a nearly 2,200 fold increase in energy consumption for the some task. When Amazon did their tests on the languages [4], they found a similar ranking, but where Python did considerably poorly:

Generally, the interpreted languages were the slowest. Once surprising thing is that Golang did not perform strongly, and had around the same energy footprint as C#. For a compiled language, this is surprising. One problem with Golang seems to relate to its garbage collector, and which slows down the execution of the program — as opposed to Rust and C which do not have any garbage collection. Rust, overall, doesn’t need a garbage collector, as the language is designed to remove objects when they are not required any more.

Conclusions

And, so, it’s clear that C, C++ and Rust are out in front of being the fastest and most efficient programming langauges for their operation. But, C brings in so many bugs — especially memory-related one — and so it’s Rust that probably wins overall. For Golang it is rather disappointing, especially as it should be faster than C# and Java, but its garbage collection seems to let it down.

Here’s Rust:

https://asecuritysite.com/rust/

And I leave you with:

may you walk gently through the world and know its beauty all the days of your life.

References

[1] Pereira, R., Couto, M., Ribeiro, F., Rua, R., Cunha, J., Fernandes, J. P., & Saraiva, J. (2017, October). Energy efficiency across programming languages: how do energy, time, and memory relate?. In Proceedings of the 10th ACM SIGPLAN International Conference on Software Language Engineering (pp. 256–267).

[2] Couto, M., Pereira, R., Ribeiro, F., Rua, R., & Saraiva, J. (2017, September). Towards a green ranking for programming languages. In Proceedings of the 21st Brazilian Symposium on Programming Languages (pp. 1–8).

[3] Pereira, R., Couto, M., Ribeiro, F., Rua, R., Cunha, J., Fernandes, J. P., & Saraiva, J. (2021). Ranking programming languages by energy efficiency. Science of Computer Programming, 205, 102609.

[4] https://aws.amazon.com/blogs/opensource/sustainability-with-rust/