Hacker Newsnew | past | comments | ask | show | jobs | submit | lkjflakjsdeowe's commentslogin

Sigh, here we go again.

> Tony Hoare's famous maxim "Premature optimization is the root of all evil."

It's actually from Donald Knuth and this quote is frequently taken out of context to argue against optimization in general.

Here is the entire quote

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

The point is to spend time optimizing where it will have impact.


Knuth attributes it to Hoare, and Hoare attributes it to Knuth. So it come's down to who you want to believe. Probably best to attribute it to both. My guess would be that Tony said it first, Knuth refined and printed it.

It's always good to have the longer quote which gives needed context.


Hoare attributed it to Dijkstra. See https://hans.gerwitz.com/2004/08/12/premature-optimization-i... .

"I’m sorry I have no recollection how this quotation came about. I might have attributed it to Edsger Dijkstra."


Someone needs to make a Spider-man meme with the quote.


Maybe it is in the end a secret deal between them, to have a joke about circular references. ; )


Also people forget that quote is from the 70s. Almost 50 years go.

Programming used to be very different from what it is now. "Premature optimization" wasn't "hey just use this popular lib cause it scales", it was "let's use some impossible to understand bit fiddling algorithm that only works on this piece of hardware".


> Programming used to be very different from what it is now

Programming has definitely evolved. This maxim seems to be exactly as applicable then as it is now though, and as misunderstood.


In any compiled language your optimizer will do all those weird things for you, and will even handle all the different generations of CPUs for you. Compilers never give you a better algorithm if you write the wrong one.

Almost all languages have a standard library that has all the normal algorithems you would want, and where something wierd is better they have that done for you.


Compilers can and do replace some (simple) algorithms with a better one.

At https://stackoverflow.com/questions/74417624/how-does-clang-... is someone asking why the compiler replaced:

    int a = 0;
    while (n--)
        a += (n * n);
an O(n) algorithm, with the O(1) equivalent of:

    a = n * (n-1) / 2 + n


I think your `n * (n-1) / 2 + n` should be `n(n+1)(2n+1)/6` according to the SO article.

`n * (n-1) / 2 + n` would be the sum of numbers, not sum of squares.


Thank you for the correction!


I don't see how the larger quote adds any additional meaningful context. Once you have identified (measured) the critical 3%, the state is no longer premature. That is already implied in "Premature optimization is the root of all evil". The the maxim is not "Optimization is the root of all evil".


> The the maxim is not "Optimization is the root of all evil".

In my experience, that's exactly how most people understand it.


Too many people take this as dogma and just don’t learn the efficient way to do things. I’ve lost count of the number of FE devs that interview in my company’s DS&A section and tell me bubble sort is the best we can do. I don’t need a derivation off the top of your head, just know a couple and tell me a good choice for the problem and I’m good… same thing here. If people live the “don’t prematurely optimize” to the point that they don’t even know the efficient ways to do things, how will they know where it’s important.


> this quote is frequently taken out of context to argue against optimization in general

Maybe it is, but that's not how it's being used in this context.


I don't see anyone taking this out of context here. The entire quote is less pithy but not different in meaning. "Premature" is literally the first word.


How is this not covered by points 1 (don't put in hacks because of guessing) and 2 (measure)?


Premature "premature optimization is the root of all evil" is the root of all evil.


> The point is to spend time optimizing where it will have impact.

Your whinging would be more appropriate if Measurement was not emphasized, right near the top.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: