premature optimization
expressionprogramming
Tuning code for speed before you know it matters, at the cost of clarity and time.
Knuth's line - 'premature optimization is the root of all evil' - warns against contorting code for performance gains that profiling has not shown you need. The full quote allows for the critical few percent that do matter; the point is to measure first, then optimize the part that actually moves the needle.
Premature optimization is the root of all evil is the most misquoted line in software, and the damage comes from the truncation. Knuth wrote that we should forget about small efficiencies about ninety-seven percent of the time, and then immediately added that we should not pass up our opportunities in that critical three percent. The sentence is an argument for measurement, not against performance work.
The mangled version has been used to justify a great deal of genuinely slow software. It gets deployed against thinking about complexity at design time, against choosing an appropriate data structure, against caring how many round trips a page makes. None of that is what Knuth meant: the target was hand-tuning arbitrary lines of code on intuition before knowing where time is actually spent.
The distinction is between optimization and design. Choosing an algorithm whose complexity suits the expected input size is design, and doing it late is expensive because it changes the shape of everything around it. Rewriting a loop to save instructions before profiling is optimization, and doing it early is usually waste. Measure first, optimize what the measurement identifies, and do not confuse either with the architectural decisions that have to be made before there is anything to measure.
Also known as: root of all evil, premature optimization is the root of all evil
Sources
- Knuth, 'Structured Programming with go to Statements' (1974), quoting Hoare