The problem
Membership checks and lookups against a list look at every item until they find a match. Inside a loop, that turns a linear job into a quadratic one.
The fix you can use today
- Use a set for 'is this in there?' checks.
- Use a dictionary or map for lookups by key.
- Build the set once, outside the loop.
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
- 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 scoring a codebase for quality, security and green software patterns in one pass, so a wasteful lookup like this comes out as a ranked defect alongside the bugs and security findings. More on this.
Tags
#algorithms#python#java
Sources
All GreenCode Tech Tips