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

Transcript

Asking AI for efficient code made it up to ninety-two percent more efficient. GreenCode researchers found that asking AI for efficient code often helps, but not every time. But there is a catch. Often, the more efficient code was less accurate. In some tests, researchers removed half the problem description. The code became over four times more efficient. But its correctness dropped to zero. Efficient but wrong is worth nothing. Here is the fix you can use today. Ask clearly for efficient code, and name the limit. For example: use less memory, or a faster method. Or give your agent energy rules to follow, like GreenCode's Constitution skill. Always run the tests before you accept it. Then measure the energy of the new code, using tools like GreenCode's green-languages. That code may run thousands of times, so it matters. GreenCode helps tackle this by publishing EnergyBench, which scores AI code for correctness and energy. Its own AI stays lean, with small models and the smallest tool for each job. That was GreenCode Tech Tip number ten. Follow for the next tip, and find more on greencode.ai/tech-tips.

All GreenCode Tech Tips · All GreenCode videos

  • Tip #10
  • For Developers
  • Length 1:15
  • Category AI and machine learning
GreenCode

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

Get in touch