← 返回规律列表
规划

过早优化

在确认性能问题之前进行优化,常常会增加复杂度而没有实际收益。

显示原始英文内容

Premature Optimization (Knuth's Optimization Principle)

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.

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.

核心含义

先写出清晰、正确、可测量的实现,再通过 profiling 或指标找到真正的瓶颈。为一个只执行一次的函数做复杂的位运算,或者为了未来可能出现的数据规模提前引入复杂结构,往往只会让代码更难理解和维护。

优化通常意味着额外的复杂度。只有当问题已经被数据证明存在,并且收益大于维护成本时,优化才值得做。

实践例子

开发者花两天优化一个启动函数,最后发现它只占总运行时间的千分之一;真正耗时的是另一处数据排序。先测量热点,再针对热点优化,通常能用更小的代价获得更大的收益。

来源与边界

这条经验通常与唐纳德·克努特的名言联系在一起。它并不是反对性能工程,而是反对没有测量依据的微优化。实时系统、容量规划和安全边界等场景仍然需要尽早考虑性能约束。