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.*

Photo by Alistair MacRobert on Unsplash

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.

Green
To make something more environmentally-friendly

How to use it?

import energyusage

By pressing the ‘tab’ key after this code we can look at what this library has to offer.

energyusage.
energyusage under the hood

Evaluate sounds cool let’s look at the docstring

Function docstring Pt.1
Function docstring Pt.2

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:

def recursive_fib(n):
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:

pdf(default = False): generates a PDF report, alongside the command-line utility

*The above report was generated for the manual and is not my own.

Photo by Riccardo Annandale on Unsplash

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:

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!

Photo by Junior Moran on Unsplash

Practicing Data Scientist. Interested in Games, Gamification, Ocean Sciences, Music, Biology.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store