Green code — Python energyusage package
TLDR: measure the environmental impact of your codes computation with the energy usage package
NB *Due to the methods in which the energy measurement is being done (through the Intel RAPL interface and NVIDIA-smi), this package is only available on Linux kernels that have the RAPL interface and/or machines with an Nvidia GPU.*
Hello everyone o/
With thanks to the people at responsible problem solving I am really pleased to be sharing with you today the awesome energyusage package for python.
This is a Python package that allows you to measure the environmental impact of computation. We will learn how to use it today.
Up until now we’ve had profilers like c profile and time it that measure how long parts of the code or function are taking to compute.
While these are very useful in their own capacity to provide insight into which parts of your code are slower or faster, they don’t tell you anything about how green your code is.
To make something more environmentally-friendly
How to use it?
By pressing the ‘tab’ key after this code we can look at what this library has to offer.
Evaluate sounds cool let’s look at the docstring
Providing the energyusage.evaluate() function a function of your own making will return an energy report. You will have to also provide the arguments that usually go along with your function, like so:
if (n <= 2): return 1
else: return recursive_fib(n-1) + recursive_fib(n-2)energyusage.evaluate(recursive_fib, 40, pdf=True)
This will return the value of your function, while also printing out the energy usage report on the command line. Optional keyword arguments:
False): generates a PDF report, alongside the command-line utility
*The above report was generated for the manual and is not my own.
This is a fantastic step in bringing the capacity for environmentally mindful coding to the many. Being able to receive an energy consumption evaluation metric such as 1.6watts used is incredible. The package even takes into account the loss of energy through wires!
My machine does not currently meet the requirements (see the top of post) to use the package, but I'm looking into making it work as a priority as using the energyusage package we will be able to have an insight into questions such as:
- How much energy does it use?
- What is the carbon footprint of my code?
- How much CPU resources vs disk vs network
I am very excited to be using this package and learning about how it works (stay tuned for more posts about this).
Lastly, I want to give my greatest appreciation to all those involved!