_- Faster C++

Eco-friendliness and C++

Posted on

Ever heard of digital pollution? It accounts for more carbon footprint than air travels at the global scale! Among those activities, how could we ignore the huge impact of the electronics industry? It is well known that electronic devices require tons and tons of mining, often using chemicals and destroying nature and wildlife habitat. In fact, there are many articles like here (French) and here which describe this problem. But I thought I could cover something a bit further, that only developers could understand and do something about. Indeed, it must not be omitted that, once a device is produced, its exploitation itself requires energy, probably from nuclear or fossil origin. Thus increasing the pollution and global warming. What is exploitation of an electronic device? This is exactly where the software comes into play. It is the software that controls what the hardware does. Through the complex transistors circuitry, it is the software which decides how much Watts will be consumed by the device. Thus, a gaming computer will burn more power when all graphics quality options are enabled. And the gamer simply has to turn off options to give tiny little breathes to Earth! Now from the development point of view, how could we impact the energy consumption of our software? It is simply by making better use of our hardware in the first place. Bad design and lack of knowledge of the hardware architecture causes losing CPU cycles. Which equals having devices turned on, but doing nothing of interest, at least from the user's point of view. Though it is certainly doing something internally however. Most probably fetching data in memory and waiting for them to be retrieved and loaded in the caches. So, as a software developer, please gain knowledge about what happens beyond the code, deep inside the hardware. Do not re-compute heavy stuff if the result could be cached. Do not reload files from the network if you could have them locally. Improve your coding skills to reduce implementation errors, which otherwise would lead you to re-run your test suite. And do not cause the instruction pipeline to be flushed too often (A future post is to come about that topic). More than ever, the Earth is exhausted from human activity. It is our duty as developer to make up for this situation as much as we can, and share the knowledge. Now go get the best of both worlds: Eco-friendly and fast programs!

What you really optimize

Posted on

As a C++ developer searching performance, you don't need to optimize the C++ code per se. What you need to optimize is the resulting binary file. Now take a deep breath to assimilate that... But you can't read the binary, could you? That is where the assembly kicks in! In between the C++ code and the binary, there is this intermediate language called assembly. Now don't get me wrong: I don't approve trying to optimize the assembly in any way. But the assembly gives a clear vision of what the binary looks like. And reading it may give you insights about what your C++ syntax or compilation flags really does to the binary. To view the assembly corresponding to your .cpp source, simply add the -S option to GCC during compilation. It will dump the assembly code in a file with the ".s" extension.



Copyright © 2022-2026
Créé par Janahan Nallanathan