Tech Tip #54: Profile before you optimise

Fixing code that never runs saves nothing.

Video created by explainapaper.com

The problem

Developers often optimise the code they suspect, not the code that is actually hot. A 50% gain in a function that takes 2% of the runtime is a 1% gain overall.

The fix you can use today

  • Profile the real workload first and find where the time and energy go.
  • Only work on paths that account for more than about 5% of the total.
  • Leave test fixtures, prototypes and code that runs a handful of times alone.

Tools that help

  • GreenCode Constitution skill (GreenCode researchers): open-source energy principles and a catalogue of 37 energy anti-patterns that an AI coding agent can follow to find and fix waste like this
  • GreenCode Constitution profiler (profile.sh) (GreenCode researchers): a script from the Constitution tooling that measures the energy of a command from processor counters, GPU sensors and an optional smart plug, and writes the result as JSON
  • green-languages (GreenCode researchers): a command-line tool that measures the energy of whole programs or marked sections of code, across languages

How GreenCode helps

GreenCode helps tackle this by building a load test from the system's own endpoints, recording energy as well as speed while it runs, and ranking the hotspots by the energy they use. More on this.

Tags

#profiling#measurement#refactoring

Sources

All GreenCode Tech Tips

  • Tip #54
  • For Developers
  • Category Code efficiency
  • Format short video
GreenCode

Want this checked on your own system? Talk to the GreenCode team.

Get in touch