Knuth's Optimization Principle
"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%."
"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%."
Most of a program's code does not matter to its speed, and time spent making that code faster is time lost twice: once in the writing, and again in every later attempt to read or debug the result. The small remainder that does matter should be found by measurement and then optimised hard.
Rated Heuristic: a dependable rule of thumb, not a proven theorem. The other 224.
Execution time in almost any non-trivial program is concentrated: a small fraction of the code accounts for most of the running time, and which fraction it is cannot reliably be guessed from reading the source. Optimising by intuition therefore mostly touches code that was never the constraint, while making it denser, more coupled and harder to change — costs that are paid on every subsequent edit. Measuring first inverts the order: it identifies the small region where the same effort buys a real improvement, and leaves the rest legible. Knuth's whole passage makes this the argument, not the slogan: the objection is to the wasted 97%, not to optimisation.
It is the most quoted sentence in programming, and the one most often quoted against its author's meaning. Read whole, it is a claim about where effort should go rather than a claim that effort is bad, and it supplies the argument for the profiler as a standard tool: not because measuring is virtuous, but because guessing is demonstrably poor.
It is advice about micro-optimisation, and it is regularly stretched to cover architectural decisions it was never about: an algorithm with the wrong complexity class, a schema that forces a full scan, or a protocol chattier than the network allows are choices measurement will confirm too late to change cheaply. The 97/3 split is rhetorical, not measured. And the whole argument assumes a profiler and a representative workload; where neither exists, 'measure first' is not available and judgement has to serve.
It is not an argument against caring about performance, and Knuth (who wrote the passage inside an article defending the judicious use of goto for exactly performance reasons) plainly did care. The operative word is premature: optimisation before measurement, not optimisation. The sentence is also almost always quoted with its second half removed, which reverses the balance of the advice.
Real as a rule of thumb, not as a theorem. Knuth's Optimization Principle is rated Heuristic here: dependable enough to plan with, and with no proof behind it.
Rated from two sources. 225 of the 1,116 entries carry this rating. How the scale works.
The best-known laws are the least likely to rest on measurement.
From Donald Knuth's "Structured Programming with go to Statements", ACM Computing Surveys 6(4), December 1974, pp. 261–301, at section 1. Fifteen years later, in "The Errors of TeX" (Software—Practice & Experience 19(7), 1989), Knuth referred to the maxim as Hoare's Dictum; C. A. R. Hoare is not documented as having originated it, and the attribution is doubtful. The 1974 wording is the one quoted here.
Everything above traces to dl.acm.org and en.wikiquote.org, two sources in all, one of them the original publication rather than an account of it. Nothing is written from memory, and the method is written down. Spot an error or a better source? Suggest a fix.