Premature optimization
"Premature optimization is the root of all evil" is one of the most quoted aphorisms in software engineering. It is attributed to Donald Knuth, who popularized it in his 1974 paper "Structured Programming with go to Statements", where he in turn credited a version of the remark to C.A.R. Hoare. The full sentence is more careful than the slogan usually repeated. "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%."
The point is not that optimization is bad. It is that optimization done too early – before there is evidence about where the costs actually lie – is bad. "Premature" is the operative word. Optimizing before measuring usually means optimizing the wrong thing. A hot path is guessed at, an allocation is assumed to matter, a data structure is chosen for a workload that turns out not to be the one the system has. The gains, if any, are paid for in code that is harder to read, harder to change, and more tightly bound to assumptions the next change will invalidate. It is a common anti-pattern and a reliable source of accidental complexity.
The remedy is the second half of Knuth’s sentence. Establish the critical 3% with evidence. Profiling identifies where time is actually spent, and performance testing confirms that a change moves the number that matters. Optimize the critical path once it is known, and measure again afterwards. Optimization without measurement is what the aphorism warns against; optimization after measurement is exactly the critical 3% Knuth is defending.
Premature optimization is often bracketed with a family of design principles that share its instinct for deferred commitment. YAGNI warns against building for hypothetical needs. Do the simplest thing asks for the minimal solution that could possibly work. The KISS principle names simplicity as a goal in its own right. All four point the same way. Solve the problem you actually have, and let the next problem reveal itself before you pay for it.
The aphorism is also misread in the other direction. "Premature optimization is the root of all evil" is sometimes shortened to "optimization is the root of all evil," which it is not. Knuth spent a career optimizing. The distinction the aphorism draws is between optimization driven by measurement and optimization driven by guesswork. Mechanical sympathy, for example, is not premature optimization: it assumes measurable goals exist and that changes are driven by profiling rather than intuition. Code that has to meet a performance budget – a tight inner loop, a real-time system, a latency-sensitive hot path – is exactly the critical 3%, and there the discipline is not to avoid optimization but to make it deliberate.
See also
- Performance
- Profiling
- Performance testing
- Mechanical sympathy
- YAGNI
- KISS principle
- Do the simplest thing that could possibly work
References
- Donald E. Knuth (1974). Structured Programming with go to Statements. ACM Computing Surveys 6 (4): 261–301.