RSS Amplifier

Laws of Software Engineering · Jun 24, 2026

Premature Optimization (Knuth's Optimization Principle)

0
Sign in to vote or save

Dr. Milan Milanović · Laws of Software Engineering

3 min read

Premature optimization is the root of all evil.

Takeaways

  • Most code doesn’t run in performance-critical hotspots, so obsessing over micro-optimizations everywhere wastes time and makes code harder to read and maintain.
  • According to Knuth, we should forget about small efficiencies about 97% of the time, and focus on clean design and correct functionality.
  • Optimized code is often more complex or less readable. If done prematurely, you incur this cost even when it’s unnecessary.
  • Get it working correctly first, then make it fast, then make it pretty.

Overview

Knuth’s Optimization Principle captures a fundamental trade-off in software engineering: performance improvements often increase complexity. Applying that trade-off before understanding where performance actually matters leads to unreadable systems.

Early in development, your focus should be on a clear design. If you optimize too soon, you might introduce bugs or inflexibility, all to speed up parts of the code that may not even be bottlenecks. It’s often cited that 20% of the code may consume 80% of the execution time (a Pareto-like notion). Optimizing anything outside that critical 20% is wasted effort and adds risk. The principle advises writing simple code, then profiling and improving only the parts that truly need it.

Premature Optimization (Knuth's Optimization Principle) illustration

Premature Optimization

Examples

A developer writes a low-level C routine with complex bit-twiddling to squeeze out speed, spending two days on it, only to find that the function is called once at startup, taking 0.001% of runtime. The effort was wasted, and the code is now more complex to understand. Meanwhile, another part of the program (such as a sorting function on a large dataset) was the real bottleneck, but remained unoptimized because time was spent on the wrong thing.

Another example is prematurely choosing a complex data structure for theoretical efficiency (say, a custom tree for log(N) lookups) when the simpler approach (like a linear search) would have been acceptable for the data sizes involved.

In contrast, following Knuth’s advice, you implement simply, measure, then find that 80% of time is spent in one loop, you optimize that loop or use a better algorithm there. This gives real improvements with minimal added complexity elsewhere.

Origins

Donald Knuth, a legendary computer scientist, made this statement in his 1974 paper “Structured Programming with Go To Statements” published in ACM Computing Surveys.

The full quote provides important context: “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%.”

He didn’t literally mean all evil, but the quote caught on because it resonates with developers who have seen codebases twisted by needless optimizations.

Over time, this quote became almost a proverb in software engineering. It’s taught to juniors: first make it work, then make it right, then (if needed) make it fast.

Further Reading

Want to go deeper?

All 63+ laws are covered with more depth, examples, and practical guidance in the Laws of Software Engineering book.

Get the Book

Last updated: June 24, 2026

Read the original on lawsofsoftwareengineering.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.