# 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%.

**Field:** Software & systems · **Reliability:** Heuristic · **Coined:** 1974 · **Named after:** Donald Knuth

*Also known as: Premature optimization is the root of all evil; The root of all evil; Hoare's Dictum*

## What it means

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.

## How it works

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.

## Examples

- **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

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.

## Where it breaks down

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.

## Common misreadings

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.

## Origin

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.

## Related

- [YAGNI](https://conyso.com/lawtome/laws/yagni/) — kindred
- [Amdahl's Law](https://conyso.com/lawtome/laws/amdahls-law/) — kindred
- [The Pareto Principle](https://conyso.com/lawtome/laws/pareto-principle/) — kindred
- [Worse Is Better](https://conyso.com/lawtome/laws/worse-is-better/) — kindred

## Sources

- [Knuth, D. E. (1974). "Structured Programming with go to Statements." ACM Computing Surveys 6(4), 261–301. — the passage, in full, at section 1.](<https://dl.acm.org/doi/10.1145/356635.356640>)
- [Wikiquote: 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.](<https://en.wikiquote.org/wiki/Donald_Knuth>)

---
Source: https://conyso.com/lawtome/laws/knuths-optimization-principle/ · The Law Tome — a sourced index of named laws.
