Warting a code with less energy

Continuing the discussion from Go and green tech - how do you use Go to save the environment?:

I just read the above and wondering how this can be done, how can I measure electricity consumption of my code? Any example of a code that container high electricity, then be rewritten to consume less energy?

very simple: each CPU operation consumes energy. Try to minimize CPU operations. At the same time, every bit of memory consumes energy, try to minimize memory usage.

IMO, at code level, it won’t help much. Energy saving is directly related to how hardware is designed and fabricated.

On consumer level side, you can consider deploying your app on ARM or RISC-V based endpoints (e.g; Vision5 - VisionFive 2: RISC-V Quad Core Low Cost SBC - YouTube). However, that itself has its own challenge to deal with.

All and all, the general Green Rules applies: Deploy only what you consume; 3R whenever possible.

2 Likes

IMHO it is a bit harder to reduce, reuse and recycle using Go. At least I have some difficulties to understand how. For example sqlx (Postgresql) can reduce code a lot - compared to other drivers, but is not considered generate statically typed code. And therefore not “idiomatic”.

But one way to achieve better code (at least on the web) is to measure and fix problems repeatedly. I myself use https://pagespeed.web.dev often to find ways to increase speed and as a result reduce overall impact. Not only the electricity. But the web has some other challenges as it is complex. (hosting, caching, decrease traffic etc)

I read somewhere that only about 10 percent of Go developers use Go for web development. Anybody can confirm this?

2 Likes

Sir, you are correct. But look at the practicality of your statement: you have a machine that has already been built, and you run your code on it. For the needs of your code, that machine is static, it will not change.

As long as your code is not designed for a specific machine, optimized to make use of its particular specifications, having more efficient code helps, even when you upgrade your machine.

Codes are merely instructions. At best, we can optimise algorithm to perform better with less cycles but the effect is minuscule. It’s like in layman terms:

  1. We need to do food rationing (staying green).
  2. I can work in English, Mandarin, etc languages (codes).
  3. I eat 2x more foods than ordinary people (needs 2x energy).
  4. At max is you can tell to me in your language that we are low in food, or point to my belly to start reducing my consumption (tells me to use 1x energy for the same output instead).

It’s not the speaking or belly-pointing that are not helping; the output it generated is very small (for the fact that I still need ~2x energy just to stay up or I will die of hunger). In this case, I will rather tackle the problem from other angles like:

  1. hire another person that eats <2x of foods but do better job than me (hardware replacement).
  2. look into self-sustainable indoor-farming sources (renewable energy powering).
  3. optimising my work schedules with another person that eats 1x food for mundane tasks and only calls me for big tasks. (scheduling optimisation, energy+performance co-processors).

The argument is mainly about tackling the problem from the correct angle.


Site-note: Don’t feel down. We (as in active maintainers) are always refactor and optimise our codes for maintainability regardless (e.g. performance optimisations, etc) so you still get better codes.

That’s where the deploy only what you consume comes in. If your machine haven’t achieve its ROI timeline (usually 6 years), continue using it. Till then, you can consider optimising using VM or containers (dockers, etc) to consolidates your computing services to a minimum of active hardware as possible. If you can do something like Google Container Engine’s autopilot feature, the better.

On the next hardware refresh, consider greener hardware like ARM-based or RISC-V servers and endpoints.

Also don’t forget to recycle the old hardware.

Not that I’m aware of.

img

Source: Go Developer Survey 2022 Q2 Results - The Go Programming Language for 5752 data sources.

2 Likes

Why do you claim that the effect is minuscule ? How do you know that, what proof do you have ?

I strongly disagree. The greenest hardware you can use is the one that you have now, much greener than to build a new one. In the same way, and second hand car is much greener than a new electric car, simply because the electric car needs to be built first.