Which programming language is fast and energy efficient? Someone compared 27 languages

When the mobile phone is almost out of power, the management software will often remind us to turn off some high-power applications. It can be seen that in addition to hardware manufacturers, software manufacturers should also pay attention to energy consumption. In this article, the researcher analyzed the energy consumption comparison of various programming languages.

Which programming language is fast and energy efficient?  Someone compared 27 languages

When energy consumption has become an important indicator, how do we choose a programming language? In 2017, a team of 6 Portuguese researchers decided to investigate this issue and published a paper entitled "Energy Efficiency Across Programming Languages". They wrote 10 problem solutions (following the same algorithm) in 27 languages, and then ran these solutions, recording the power consumption, speed and memory usage of each programming language, and the sorting results obtained are shown in the following figure:

Which programming language is fast and energy efficient?  Someone compared 27 languages

Through this research, they came to some interesting conclusions, for example, a language that runs fast may not necessarily consume less energy.

In the paper, the author also analyzed the results according to the execution type of the programming language (compilation, virtual machine or interpretation) and programming paradigm (imperative, functional, object-oriented, and scripting).

Specifically, the researchers used 10 questions from Computer Language Benchmarks Game (a free software project for comparing performance that contains a set of standard algorithm problems and a framework for running tests) to make the research more comparable Sexuality and representativeness.

Running various benchmark tests is very important because their results will vary depending on the tests performed. For example, overall, the C language is considered the fastest and the most energy efficient. But in benchmark tests involving scanning specific gene sequences in a DNA database, Rust is the most energy-efficient, while C language ranks third.

Even in the same test, the "best" language depends on your criteria. For example, in a test where C ranked second and Rust ranked first, if the results were sorted by memory usage, Rust would drop 9 places. In a test with the second most energy-efficient Fortran, if the results are sorted by running time, it also drops 6 places.

The researchers pointed out that they strictly abide by the CLBG project guidelines regarding compiler versions and best optimization flags. The power consumption is measured using a tool from Intel-Running Average Power Limit, and each program is run 10 times. The author said, "This is to reduce the impact of cold start and cache effects, and to be able to analyze the consistency of the measurement to avoid outliers."

To enhance consistency, all tests were run on a desktop computer running Linux Ubuntu Server 16.10 (kernel version 4.8.0-22-generic).

Are fast languages ​​more energy efficient?

This paper carefully studied a common assumption: the faster the program consumes less energy. The researchers pointed out that, in fact, this is not as simple as the physics law E(nergy) = T(ime) x P(ower). This is because electricity is not consumed at a certain rate. In a study by other researchers, the running time of a Chapel program was 55% less than another equivalent program written in Pascal, but the measurement results showed that the energy used by the Pascal program was 10% less.

Therefore, although it is generally believed that energy consumption decreases when programs run faster, the researchers clearly pointed out that "faster languages ​​are not always more energy efficient."

This can be a difficult question to answer because power consumption is affected by many factors (including the quality of the compiler and the libraries used). But in the end, the researchers concluded that whether it is a compiled language, an interpreted language or a virtual machine, most of the energy (about 88% on average) is actually consumed by the CPU.

After analyzing and studying the experimental results, the researchers also concluded that there is "almost no relationship" between the peak usage of DRAM and energy consumption. The research provides an answer to a long-standing question: "Is faster speed more environmentally friendly?" Yes, it is true. When sorted by execution time, the top 5 most energy-efficient languages ​​are still ranked in the top 5 , And the difference in energy and time values ​​is small.

In fact, in the 10 benchmark question tests, 9 of the highest scores (both speed and energy efficiency) are one of the three languages ​​with the fastest speed and the highest energy efficiency, which did not surprise researchers. As we all know, just like the data in the research shows, the three top-ranked languages, C, C++, and Rust, have undergone a lot of optimizations and are more efficient in execution performance.

But when ranking the other 24 languages ​​by running time, the situation is different. Only 4 languages ​​maintain the same energy and time rankings (OCaml, Haskel, Racket, and Python), while the remaining languages ​​are completely scattered.

Even in a single benchmark test, there are languages ​​that are fast but not energy efficient.

Advantages of compiled languages

Compiled languages ​​seem to be the most energy-efficient and fastest-running languages, and authors can even use numbers to quantify this difference in their papers.

On average, a compiled language requires 120J of energy to run the solution, while a virtual machine and interpreted language cost 576J and 2365J, respectively.

When comparing the execution time, the researchers also used the same accuracy and concluded that on average, the compiled language spent 5103 milliseconds, the virtual machine language spent 20623 milliseconds, and the interpreted language spent 87614 milliseconds.

The top 4 in the two rankings are all compiled languages ​​(with one exception, the Java language).

Which programming language is fast and energy efficient?  Someone compared 27 languages

The five slowest languages ​​are all interpreted languages: Lua, Python, Perl, Ruby, and Typescript. The five most energy-intensive languages ​​are also interpreted languages: Perl, Python, Ruby, JRuby and Lua.

But at the same time, when using regular expressions to manipulate strings, three of the five most energy-efficient languages ​​(TypeScript, JavaScript, and PHP) are interpreted languages. Although in other cases, they are often not very energy efficient.

Compiled languages ​​also occupies the top 5 rankings with the least memory.

Which programming language is fast and energy efficient?  Someone compared 27 languages

The researcher said: "On average, compiled languages ​​require 125Mb, virtual machine languages ​​require 285Mb, and interpreted languages ​​require 426Mb." Specifically, 4 interpreted languages ​​are at the bottom of this ranking, which means they consume Has the most memory space.

"If you sort by their programming paradigm, imperative languages ​​need 116Mb, object-oriented languages ​​need 249Mb, functional programming languages ​​are 251Mb, and scripts need 421Mb."

In fact, when comparing different paradigms, imperative programming usually comes first. Compared with benchmark programs in object-oriented, functional and scripting paradigms, benchmark programs in imperative programming use much less energy on average and run faster.

Which programming language is fast and energy efficient?  Someone compared 27 languages

But there are many factors to consider. The researcher said, "It is clear that different programming paradigms, and even languages ​​in the same paradigm, have completely different effects on energy consumption, time, and memory." However, which of these factors is the most important depends on your own needs (for example, background tasks do not always require the fastest running time).

Some applications need to consider two factors, such as energy consumption and execution time. The researcher said: "In this case, the C language is the best solution, because it has advantages in both aspects." If you want to save time while saving memory, then C language, Pascal and Go language is fine. If you are observing 3 variables (time, energy consumption and memory usage), the conclusion is the same. However, if you just want to save energy while using less memory, then the best choice is C or Pascal.

Which programming language is fast and energy efficient?  Someone compared 27 languages

Guess you like

Origin blog.csdn.net/aizhushou/article/details/109326115