Somewhere in your career, probably in a code review, someone will say “premature optimisation is the root of all evil” and the discussion will end. The phrase is treated as a licence: write it simply, deal with speed later, and anyone who asks about performance is being fussy. That reading is popular. It is also wrong in two different directions, and it is worth taking apart.
What was actually written
The line comes from Donald Knuth’s 1974 paper “Structured Programming with go to Statements”. The sentence around it says that we should forget about small efficiencies, say about 97 percent of the time, and that we should not pass up our opportunities in the critical 3 percent. Two things stand out. The target is small efficiencies, such as replacing a multiplication with a shift or hand-unrolling a loop. And the passage ends by telling you to optimise, once you know where it counts.
So the warning is not “do not think about performance”. It is “do not spend effort on speed where you have no evidence it matters”. The word that carries the meaning is premature: before you have measured.
Myth one: you can always fix it later
Some decisions are cheap to change, and some are expensive, and speed depends heavily on the expensive ones. Consider a few of them:
- Whether the page makes one call to the database or one call per row.
- Whether a list is searched by scanning it or looked up by key.
- Whether two services talk through a chatty API with fifty round trips or a single batched one.
- Whether the data model lets you fetch what you need without joining six tables.
None of these is a micro-optimisation. They are design decisions with a performance consequence, and changing them after launch means changing contracts, schemas and every caller. Thinking about whether an approach will still work at ten times today’s data is not premature. It is ordinary engineering. A quick back-of-the-envelope estimate, say how many rows, how many requests per second, how much memory, takes ten minutes and rules out the designs that cannot work.
Myth two: you know where the time goes
The other half of the mistake is the programmer who spends a week making a function faster because it looks slow. Intuition about hot spots is poor, including among experienced people. Consider an invented but typical case: a report page in a logistics tool takes 4 seconds. A developer in Coimbatore notices a clumsy string-formatting loop and rewrites it to run twice as fast. Then a profiler shows where the 4 seconds went: 3.2 seconds waiting on one database query, 0.2 seconds in that loop, the rest in rendering and network. Halving the loop saves 0.1 second. The page is now 3.9 seconds. Adding an index to the query could take it to well under a second.
There is a tidy rule behind this, often called Amdahl’s law. If a part of the program accounts for a fraction p of the total time, then making that part infinitely fast cannot save more than that fraction. A step that takes 5 percent of the time can give you at most about a 5 percent improvement, however clever you are. So the first question is never “how do I make this faster” but “how much of the total is this”.
A sensible order of work
Write the clear version first, with decent basic choices: the right data structure, no query inside a loop, no needless copying of large data. That is not optimisation; it is just not being careless. Define what “fast enough” means with a number, such as 95 percent of requests under 800 ms. If you already meet it, stop. If not, profile with real or realistic data, find the biggest single cost, fix that one, and measure again. Repeat until you hit the target, then stop for real.
Keep the measurement. A before-and-after number in the pull request description makes the change defensible and tells the next person it was worth it. Optimisations without numbers tend to survive as folklore long after the reason is gone.
The cost on the other side
Optimised code is usually harder to read: caches that need invalidating, hand-managed buffers, tricks that depend on how a particular compiler behaves. Every such trick is a maintenance bill. That is the real argument in Knuth’s sentence. You pay a permanent cost in clarity, so it needs to buy something you have verified.
Used well, then, the phrase asks you to be disciplined in both directions. Do not pour effort into speed you have not measured, and do not use the phrase as cover for a design you already suspect will not scale. A reviewer who says “show me the profile” and a reviewer who says “have you estimated the data volume” are both applying the same principle.
Figures and names in the examples are illustrative.
Leave a Reply