---
title: "Tech Tip #54: Profile before you optimise"
description: "Fixing code that never runs saves nothing."
url: https://greencode.ai/tech-tips/54-profile-before-you-optimise/
---

# Tech Tip #54: Profile before you optimise

**Fixing code that never runs saves nothing.**

[Watch the video](https://media.greencode.ai/shorts/profile-before-you-optimise-97451ff6e6de.mp4)

Video created by [explainapaper.com](https://explainapaper.com/?utm_source=greencode.ai&utm_medium=film&utm_campaign=made-by)

### 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](https://greencode-constitution.org/?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode) (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)](https://greencode-constitution.org/skill.md?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode) (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](https://github.com/PLEnergyDev/green-languages?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode) (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](https://greencode.ai/feature/automated-performance-testing/).

### Tags

#profiling#measurement#refactoring

### Sources

- [GreenCode Constitution skill, energy anti-patterns: scope guards: profile the target workload first; target code paths above 5% of energy or runtime; skip paths executed fewer than 100 times](https://greencode-constitution.org/skill.md?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode)
- [Software Sustainability Institute, Writing Energy Efficient Code: profilers (PyInstrument, gprof, Score-P) and CodeCarbon](https://www.software.ac.uk/guide/writing-energy-efficient-code?utm_source=greencode.ai&utm_medium=referral&utm_campaign=greencode)

[All GreenCode Tech Tips](https://greencode.ai/tech-tips/)

#### Key Facts

- Tip **#54**
- For **Developers**
- Category **Code efficiency**
- Format **short video**
![GreenCode](https://greencode.ai/wp-content/uploads/2024/12/greencode-logo-website-small-colours-2.webp)

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

[Get in touch](https://greencode.ai/contact/)
