Tech Tip #10: Asking AI for efficient code works, until it doesn't

Asking AI for efficient code made it up to ninety-two percent more efficient.

Video created by explainapaper.com

The problem

LLMs write code that passes tests but can be wasteful. GreenCode's EnergyBench found prompt changes can make generated code up to 91.9% more energy-efficient, but often at the cost of accuracy; in some cases removing half the problem description made code over four times more efficient while correctness dropped to zero.

The fix you can use today

  • Ask explicitly for efficient code and name the constraint (memory, allocations, complexity).
  • Always run the tests: efficient-but-wrong is worth nothing.
  • Measure the generated code's energy before you accept it.

GreenCode researchers found that asking AI for efficient code often helps, but not every time.

Tools that help

  • EnergyBench (GreenCode researchers): an MIT-licensed benchmark that scores LLM-generated code for correctness and for the energy it actually uses
  • 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
  • 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 publishing EnergyBench, a benchmark that scores AI-written code for correctness and for the energy it uses. GreenCode also keeps its own AI lean, with small models and each request sent to the smallest capable component. More on this.

Tags

#llm#ai-assistants#testing

Sources

All GreenCode Tech Tips

  • Tip #10
  • For Developers
  • Category AI and machine learning
  • Format short video
GreenCode

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

Get in touch