Saving energy and going green are already well known topics in today's world. The green movement seems to have taken over the world, empowering eco-friendly people every day. While there are multiple reasons of saving energy, here are the top 5:
- Environmental benefits
- Save money
- Health benefits
- Using less resources
- Knowledge of doing the right thing
In every domain we can analyze and make adjustments to reduce energy consumption impact on the environment, and IT is no exception.
Learning a programming language and becoming an expert in that language is not easy. Moreover, although most developers know several programming languages, we consider ourselves experts in one or two.
When deciding on a new programming language, we usually take into account things like syntax, its learning curve, documentation or whether it is widely used or not. But have we wondered which programming language is more sustainable and consumes less energy? Actually, we are not used to taking this into account when choosing between C#, Python, Java, or any other language.
Recently, a group of Portuguese researchers set out to investigate the energy consumption of up to 27 of today’s most popular programming languages, subsequently presenting their findings at the International Conference on Software Language Engineering.
“This paper presents a study of the runtime, memory usage and energy consumption of twenty seven well-known software languages. We monitor the performance of such languages using ten different programming problems, expressed in each of the languages. Our results show interesting findings, such as, slower/faster languages consuming less/more energy, and how memory usage influences energy consumption.”
According to their research, they monitored the performance of these languages using different programming problems for which they relied on different algorithms compiled by the “Computer Language Benchmark Game” project, dedicated to implementing algorithms in different languages.
The team used Intel’s Running Average Power Limit (RAPL) tool to measure power consumption, which can provide very accurate power consumption estimates.
The research shows that several factors influence energy consumption, as expected. The speed at which they are executed in the energy consumption is usually decisive. Apart from execution time, there are more factors that come into the equation when calculating the energy consumption, memory usage being such an example.
In this ranking, we can see that the most efficient programming languages are: C, C++, Rust and Java
The results are similar to the energy expense, the faster a programming language is, the less energy it expends.
Based on the study above, the results show us that:
Most environmentally friendly languages: C, Rust and C++
Least environmentally friendly languages: Ruby, Python and Perl
Although this study may seem curious, we can use this new parameter when choosing a programming language for future projects.
It also helps to consider, when choosing your “next-to-master” programming language, besides energy consumption, different other aspects, such as the available features and support or memory optimization.
By taking a little time to dive deeper into the pros and cons, you can compare and select the right language for your objectives.
There are several languages that emerged recently and are worth mentioning, two of them being Golang and Kotlin. Java has been around for a long time, so we will make a comparison with those newly created languages, to have a good overview between them.
Golang or Go is an open-source language from Google made public in 2011. The syntax of Golang is similar to the ‘C’ language because it was built in ‘C’.
Both Golang and Java are high-performing server-side languages providing ample features, but Java is more suitable for complex tasks, as Golang is not an OOP language, making it less flexible than Java.
When comparing Golang with Java in terms of performance, Golang surpasses Java. This is mainly because Java is using Java’s virtual machine, critical for code compilation. Using the virtual machine, it can run on any platform. On the other hand, Golang does not need a virtual machine, it directly compiles the code into binary code before executing the program.
The memory usage in Golang contributes to its speed, the automatic garbage collection eliminating the need for pausing.
In conclusion, Java and Golang are both powerful with significant differences: while Golang runs faster and it is better in terms of memory usage, Java has more features and better support.
Kotlin, on the other hand, compiles into the same JVM bytecode as Java. Are there any performance penalties for choosing Kotlin instead of Java?
As expected, Kotlin’s performance is similar to Java’s performance, but there are small gains in some places, like inline functions for Kotlin instead of lambdas for Java. It is noticeable that inline functions that take other functions or lambdas as parameters are very useful.
Kotlin offers almost the same runtime performance as Java.
The CPU load measurement results show that there is no significant difference between Java and Kotlin Language.
There are also benchmarks where Java and Kotlin achieved very similar results (less than 1% difference).
Based on this information we can assume that both languages use the CPU in a similar manner. From the execution time perspective, the difference between the highest and the lowest median elapsed time can vary from 6.7% in favor of Java, to 1.2% in favor of Kotlin.
Java code reached the best median time in 4 out of 6 benchmarks, the remaining performed better using Kotlin.
Memory consumption comparison appeared the lowest for Java implementations in 4 out of 6 benchmarks, in the other 2 cases they were almost equal, slightly better result for Kotlin. The best memory consumption results for both implementations do not overlap with the best execution time results. In terms of memory consumption, Kotlin goes on the second position, never achieving a better result than Java.
Based on those, Java implementation is usually better at managing the memory optimization and program execution.
It is certain that energy consumption is a parameter that can no longer be ignored in the future or even for the present when choosing the right programming language to reach your objectives.
Besides, it is worth keeping in mind that the fastest languages are generally the most environmentally friendly.
Thanks to the Portuguese researchers, we now have another criterion to take into account when choosing a programming language and a new tool to help our planet in a computerized world where millions of machines are continuously running our code.