← All anti-patterns

Software engineering / Methodological

Premature optimization

Typically, optimization involves choosing the best overall algorithms and data structures. Frequently, algorithmic improvements can cause performance improvements of several orders of magnitude instead of micro-optimizations, which rarely improve performance by more than a few percent.

This entry presents the relevant section of the broader Wikipedia article “Program optimization”.

Typically, optimization involves choosing the best overall algorithms and data structures. Frequently, algorithmic improvements can cause performance improvements of several orders of magnitude instead of micro-optimizations, which rarely improve performance by more than a few percent. If one waits to optimize until the end of the development cycle, then changing the algorithm could require a significant rewrite.

Frequently, micro-optimization can reduce readability and complicate programs or systems. That can make programs more difficult to maintain and debug.

Donald Knuth made the following two statements on optimization:

"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 also attributed the quote to Tony Hoare several years later, although this might have been an error as Hoare disclaims having coined the phrase.)

"In established engineering disciplines a 12% improvement, easily obtained, is never considered marginal and I believe the same viewpoint should prevail in software engineering"

"Premature optimization" is often used as a rallying cry against all optimization in all situations for all purposes. Frequently, Clean Code causes code to be more complicated than simpler more efficient code.

When deciding what to optimize, Amdahl's Law should be used to proritize parts based on the actual time spent in a certain part, which is not always clear from looking at the code without a performance analysis.

In practice, it is often necessary to keep performance goals in mind when first designing software, yet programmers must balance various tradeoffs. Development cost is significant, and hardware is fast.

Modern compilers are efficient enough that the intended performance increases sometimes fail to materialize. Since compilers perform many automatic optimizations, some optimizations may yield an identical executable. Also, sometimes hardware may reduce the impact of micro-optimization. For example, hardware may cache data that is cached at a software level.

Recurring responses shape the systems we live in.
Recognising the pattern is the beginning of change.

Selected Works

My first projectProject type