News for hackers. Stuff that matters.
Re:Loops in Python – Comparison and Performance by eesmith on Thursday August 08, 2019 @08:38AM (#20643933)
The 'premature optimization is the root of all evil' quote is from Knuth's 1974 paper "Structured Programming With Go To Statements", p268, second column not "The Art of Computer Programming"
Re:Loops in Python – Comparison and Performance by nine_k on Thursday August 08, 2019 @08:44AM (#20643968)
Sometimes optimization is not premature: when you have a known performance problem, have done proper profiling, and see that it's a tight loop (as usual).

Then you can shave off some execution time by using a slightly different construct, though bigger wins usually lie elsewhere.

Re:Loops in Python – Comparison and Performance by Ididntdothis on Thursday August 08, 2019 @08:52AM (#20644016)
That’s just optimization. Premature optimization is to use certain constructs that often make code more complex because you believe that they are faster.
Re:Loops in Python – Comparison and Performance by magicalhippo on Thursday August 08, 2019 @10:59AM (#20645165)
If you've got a known performance problem it's not premature. Knuths quote is the programming version of don't buy clothes for your baby before you've had sex for the first time.

There are some important distinctions though, which gets lost if one takes the quote at face value without much context. For example, I think one should strive to avoid O(n^2) loops and similar unless one is absolutely positively certain n will forever be bounded to some sane value.

My point being that O(n^2) code can make the application completely unusable, which makes it something more than a pure optimization problem.

Re:Loops in Python – Comparison and Performance by eesmith on Thursday August 08, 2019 @01:50PM (#20646773)
(As an aside, some asexual people, who never had sex and are not interested in ever having sex, are still interested in adopting, and would buy clothes for their baby before having sex for the first time.)
Re:Loops in Python – Comparison and Performance by eesmith on Thursday August 08, 2019 @09:26AM (#20644292)
Sure. But that's not premature.

Bear in mind that the given quote is only the most memorable part of Knuth's longer statement:

> 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%.

FWIW, I've read that Knuth attributes it to Hoare, and Hoare attributes it to Knuth.

Looking now, I see that Knuth also wrote a similar comment in "Computer Programming as an Art", at http://faculty.salisbury.edu/~xswang/Research/Papers/turning... :

> The real problem is that programmers have spent far too much time worrying about efficiency in the wrong places and at the wrong times; premature optimization is the root of all evil (or at least most of it) in programming.

That was published the same year and month as the first paper, so I don't know which gets priority for any attributions.