Premature Optimization

Premature optimization, (probably) because of Donald Knuth’s famous line “premature optimization is the root of all evil,” (see Structured Programming with go to Statements) is, at the very least, a controversial topic. The misconception based on that particular quote is that a programmer, while writing code, should ignore optimization altogether. In my opinion this is wrong. To put the quote in context, let’s have a look to at the text that precedes and follows it:

There is no doubt that the grail of efficiency leads to abuse. Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. 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%. A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only after that code has been identified. It is often a mistake to make a priori judgments about what parts of a program are really critical, since the universal experience of programmers who have been using measurement tools has been that their intuitive guesses fail.

My take on Knuth’s paper is that programmers, when writing code, should not care about micro optimization that has local impact only. Instead, they should care about optimizations that have global impact, like the design of a system, the algorithms used to implement the required functionality, or in which layer (SQL, PL/SQL, application language) and with which features a specific processing should be performed. Local optimizations are deferred till a measurement tool points out that a specific part of the code is spending too much time executing. And, because the optimization is local, there is no impact on the overall design of the system.

2 Comments

  1. Laimis N's Gravatar Laimis N
    March 15, 2013    

    >Donald Knuth’s famous line “premature optimization is the root of all evil,” (see Structured Programming with go to Statements) is, at the very least, a controversial topic

    Controvercial is the key here! Doland Knuth has used a standard method to draw attention to his statement. This kind of thing is widely used by all writers, journalists, etc.

    Let’s agree that expression like “root of all evil” as far as computers are concerned is quite an artistic one. It has little to do with rigid science. It has to do with conveying ideas to us, humans :)

  2. Peter H.'s Gravatar Peter H.
    April 13, 2013    

    > “Instead, they should care about optimizations that have global impact, like the design of a system, the algorithms used to implement the required functionality, or in which layer (SQL, PL/SQL, application language) and with which features a specific processing should be performed.”

    Amen to that ! Yet so few project managers and system architects seemed to care when I told them every time a project was starting that they should not plan for the (optional, at best, if there’s still a project budget to spend then) performance tuning for the final stages of a project, but that they should consider them from the very start of the project.

    Well, there’s the struggle for a clean and well performing code on one side, and there’s a project budget on the other side. Compromises, all the time.

  1. Latest data Industry news round up, Log Buffer #311 on March 18, 2013 at 15:32
  2. A Quibble With Martin’s “Optimize Later” Notion | Codecraft on March 27, 2013 at 20:08

Leave a Reply

Your email address will not be published. Required fields are marked *