Sustainable web design
Which programming language is the greenest?
JavaScript may be the most widely used programming language in the world, but that doesn’t necessarily mean it’s the fastest or most efficient. It’s all about speaking the right language for your task…
Leading academics from universities in Portugal (Lisbon, Coimbra, and Minho) evaluated 27 of the most commonly used programming languages. The hypothesis: faster languages are more efficient. The conclusion? Generally, yes, but ‘this is not always the case’.
Languages were evaluated across ten different tasks. Researchers measured how long the resultant programme took to run, how much memory it consumed, and how much energy it used. Languages were then ranked according to their performance across the three variables. You can read the full study here (https://greenlab.di.uminho.pt/wp-content/uploads/2017/10/sleFinal.pdf) but the key findings are interesting:
1. The majority of all websites use some form of JavaScript
2. JavaScript is the 17th most energy efficient and 16th fastest of 27 selected languages
3. The programming language, Go, is the 7th fastest, yet the 14th most energy efficient
4. C is the fastest and most energy efficient programming language
5. The top five languages rated equally for both speed and energy efficiency (in all cases the positions are the same for both variables, i.e. the 2nd fastest is also the 2nd most energy efficient)
6. The top five languages are (descending): C, Rust, C++, Ada, Java
The question: why, then, are websites written in JavaScript?
In short, certain languages are preferred for certain applications. A complete answer is beyond the scope of this article, but on the face of it, there are other factors besides efficiency which are important when selecting a programming language for application. Ease of use, for example, or compatibility are factors.
Attributes which make a language easier (or quicker) for the user are typically attributes that make the language slower in operation. This means that there is a necessary trade-off between developer time and machine time. Python, for example, could be considered one of the quickest languages to use. A single command may be written in Python using one line; in C, however, the equivalent command may require six lines.
Here’s an example using the command to display the phrase ‘Hello World’ on screen:
Python
print (“Hello World”)
C
#include
int main(int argc, char **argv)
{
printf(“Hello World\n”);
return 0;
}
With this in mind, to consider energy efficiency as a whole, it may be useful to consider factors beyond the energy consumption of the code itself. To write a programme in a compiled language like C, if we use the above example as a paradigm, might be much less efficient than an interpreted language like Python, in terms of developer time.
And today, there are much wider considerations in terms of energy efficiency: how does the programmer travel to work? How energy efficient is the computer they use to write their code? Is their workspace powered by green energy?
Surely the energy efficiency improvements from the compiled language outweigh the increased developer time?
It would depend how long the programme might run, and on how great a scale. If it is a large-scale programme, to be used by many people, built comparatively quickly by only a small team of developers, then you’d have to say ‘Yes’. But what about a programme that may run only once, used by comparatively few, and built by a large team of programmers? The point here: to determine energy efficiency of any programming language, developer time might be an important consideration for decision-makers.
Why do programming languages vary in energy efficiency anyway?
Or to put it another way, why are certain languages so much greener than others? (C’s rating for energy efficiency was 1.00, Python’s 75.88 – the lower the rating, the more efficient the language.) The answer relies on the difference between compiled and interpreted languages.
The difference can be more clearly illustrated by an example: you have a robot assistant who peels your melons. The robot does not speak your language. To issue the command, ‘Peel!’, you can either: (i) issue the command in a sort of intermediate language where the command is partially pre-translated by you, and then more easily understood by the robot, or (ii) you can shout the command in English, which the robot then translates itself. The second process is slower, but requires less work on your part.
7. Of the top ten languages for both speed and efficiency, eight are compiled languages
8. The highest ranked interpreted languages for speed and efficiency are 16th (Dart) and 17th (JavaScript) respectively
Why are some computer programming languages still more popular than others?
In determining the language for the application, popularity of the language may also be an important factor to consider. More popular languages may be more attractive to learn and use because they allow for a wider application, and perhaps more opportunity to work with other programmers.
9. According to the TIOBE Index, the most popular language in October 2022 is Python, followed by C and then Java.
10. The language with the highest annual gain in popularity in 2018, 2020, and 2021 was Python.
The TIOBE index determines popularity based on the number of searches for each language across seven websites and browsers. Languages selected for a particular application may be chosen because of their popularity within that particular application. When building a website, you’ll most likely use JavaScript for many reasons, primarily because 98% of all websites use JavaScript.
The most popular languages, according to TIOBE, are those which are older and more established. Python, C, Java, and C++ were originally developed in 1991, 1972, 1995, and 1985 respectively. A newer language currently gaining in popularity is Rust, which, with its borrow checker, makes it more difficult to write unsafe code. And other newer languages, such as Zig and Odin, are seen as alternatives to C.
In conclusion, it is too broad to talk about the ‘best’ or ‘greenest’ programming language. There is the fastest, the most efficient, and the most popular. The key factor is the not the language but the application of the language. A language best for the task is the primary question: questions of efficiency, popularity, ease-of-use may be only secondary considerations.
Are you a developer working on sustainable web design? Contact Shaun on info@sozodesign.co.uk if you’re interested in working with SOZO.
Looking for a digital agency to become your long-term partner to help your business succeed online?
Well say hello then!