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

Transcript

Fixing code that never runs saves nothing. Most of us optimise the code we suspect. Not the code that is actually busy. Say you make one function twice as fast. If it only takes two percent of the run time, you save one percent. All that work, and the meter barely moves. And every change adds a little risk. Here is the fix you can use today. First, profile the real workload. GreenCode's Constitution skill tells AI coding agents to do this first. Find where the time and the energy actually go. Then only work on the parts that take more than about five percent. Leave test code, prototypes and rarely used code alone. Measure each change with the Constitution's profiler or green-languages, and keep what saves. GreenCode helps tackle this by building a load test from your system's own endpoints. It records energy as well as speed, and ranks the hotspots by energy. That was GreenCode Tech Tip number fifty-four. Follow for the next tip, and find more on greencode.ai/tech-tips.

All GreenCode Tech Tips · All GreenCode videos

  • Tip #54
  • For Developers
  • Length 1:06
  • Category Code efficiency
GreenCode

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

Get in touch