Vol. I a living index of named laws · est. mmxxvi no ads · no account
The Law Tomeindex of named laws

Knuth's Optimization Principle

also known as — Premature optimization is the root of all evil, The root of all evil, Hoare's Dictum
"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%."
In plain English

What does Knuth's Optimization Principle mean?

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.

Reliability

Rated Heuristic: a dependable rule of thumb, not a proven theorem. The other 224.

Lineage
1974COINED
How it works

How does Knuth's Optimization Principle work?

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.

Where you'll see it

What are examples of Knuth's Optimization Principle?

The hand-unrolled loop A loop is rewritten for speed and becomes unreadable. It runs once per request, takes microseconds, and sits behind a database call taking milliseconds: the saving is invisible and the cost is permanent.
The profiler's surprise A team certain the bottleneck is their parser measures it and finds the time is going to logging. This is the ordinary result of profiling, not the exception, which is the point.
The 3% that pays The other half of the sentence, usually dropped. An inner loop in a codec, a hot path in an allocator, an index on the query that runs a million times a day: all worth doing at any cost in readability, because measurement showed they are where the time is.
Why it matters

Why does Knuth's Optimization Principle matter?

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.

Working with it

How do you apply Knuth's Optimization Principle?

  • Measure before you touch anything — a profile turns the question from 'what looks slow' into 'what is slow', and the two answers routinely disagree.
  • Optimise the critical 3% without apology — the sentence explicitly says not to pass those up. Once measurement has found the hot path, unreadable code there is a fair trade: with a comment saying why.
  • Separate design choices from micro-optimisation — picking the right data structure or avoiding a quadratic algorithm is not premature optimisation; it is design, and it is far more expensive to retrofit than a tight loop is.
Where it breaks down

What are the limits of Knuth's Optimization Principle?

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.

What it doesn't say

What are common misconceptions about Knuth's Optimization Principle?

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.

Is it real?

Is Knuth's Optimization Principle real?

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.

25 of 69entries in software & systems carry the same rating

The best-known laws are the least likely to rest on measurement.

Origin

Where did Knuth's Optimization Principle come from?

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.

Sources

Sources & further reading

  1. 1Knuth, D. E. (1974). "Structured Programming with go to Statements." ACM Computing Surveys 6(4), 261–301. — the passage, in full, at section 1.primary
  2. 2Wikiquote: Donald Knuth — the full quotation with its surrounding sentences, the 1974 citation, and Knuth's later reference to it as Hoare's Dictum in "The Errors of TeX" (1989), noted there as a doubtful attribution.secondary

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.

What we checked

  • 1,116entries in the tomeListed one by one on the browse page.
  • 1,116of them citing a sourceOpen any entry: its sources sit at the foot of the page.
  • 2,489source references in allEach is named, and linked wherever the source is online.