Hello everyone,
I bumped in my Hackaday news feed into following "study" regarding energy efficiencies of programming languages:
https://sites.google.com/view/energy-efficiency-languages/
(Hackaday article https://hackaday.com/2021/11/18/c-is-the-greenest-programming-language/ )
Could one state that Nim is automatically in par with C?
If one has some spare time and enthusiasm/skills (i lack at least 2 of those), would it be worth the effort to implement the test procedures in their repo to gain more visibility to Nim?
Could one state that Nim is automatically in par with C?
I'd say normal-written Nim would be worse than C, but not by much.
I actually took a course on energy efficient programming at university. Fascinating subject, and surprisingly tightly linked to speed (less energy to get work done often means work is done faster). While C and Nim definitely have a huge benefit over languages like Python or Java as they can go much lower level and has fewer abstraction there are a few things worth noting. In my course we only used C, the kinds of optimizations we were doing wouldn't have been possible in a higher level language (Nim should've worked just fine). The kinds of things we where discussing was mostly around algorithms and CPU caches. For example storing things in consecutive sections of memory instead of e.g. a linked list is in general a huge benefit. This is because the CPU will pre-load the memory following an address into its caches so that if you're accessing nearby data (which you often are if looking at other fields of the same object, or other elements in an array). This means that something like a Nim seq is possibly more memory efficient than the linked lists you will typically find in a C project. Of course we can also extend this to other data structures such as tables, and here Nim doesn't make any special attempt at optimizing for power consumption (although it does use a sequence under the hood, so it might still be better than some naïve linked list implementations).
With that being said, since Nim offers great compile-time execution possibilities you can write a lot of things which will be run once during compilation, and then uses zero power on runtime. This is a great benefit if you want to do power-efficient programming. Since it also has great meta-programming capabilities you can create green data-structures and algorithms with a nice interface so that it feels just as convenient to use as your "normal" constructs. These two things combined should definitely put Nim on the radar for anyone who wants to do power-efficient programming.
such as tables, and here Nim doesn't make any special attempt at optimizing for power consumption
Actually, the stdlib Table uses open addressed hash tables with linear probing which is an attempt to "work with" as opposed to "against" the CPU prefetcher. (How effective this is depends upon sizes of key,values, sizes of cache lines, quality of hash functions, and "load" on the table.)
Thank you for good explanations! :) There wasn't any opinions regarding my second question, but can we agree that the energy efficiency in software is important and the efficiency of the language is the last "oompf" after algos are selected and optimized properly?
This and the fact that Nim could perform great would probably suggest that Nim taking part in test/benchmarks like the one posted could be beneficial and lure even some companies who are energy efficiency -aware (Cryptocurrencies and clouds/servers in general). Is the benchmark I posted relevant, that I cannot say...
Rather than comparing languages in absolute, I think it's worthwhile to consider energy (or computational) efficiency achieved in given amounts of developer working time. Here Nim already has a advantage in terms of productivity.
Add to that compile-time execution, templates and macros and it becomes much easier to optimize applications quickly.
As a side note, I found it useful to think that code is not "faster" or "slower". The CPU always runs at the same speed. You save time and energy by taking the shortest route to your destination.