39

Several questions on SO that ask, "Which of these is faster?" or "Why is this faster than the alternative?" have one or more answers explaining the downsides of premature optimization and suggest the user profile their code to identify bottlenecks first.

I am (personally) disappointed seeing "Premature optimization is bad!" answers get up-voted on questions that are:

  1. Generally reasonable.
  2. Interesting! Performance comparisons are fun to read about, even if they're not often useful. This is the internet. (Pictures of cats are not particularly useful ;-)

Could we have the first person inclined to mention premature optimization in their answer reference the Is premature optimization really the root of all evil? question in a separate community-wiki-mode answer?

By doing so, we'd be centralizing our information a bit (DRY) allowing people to vote the premature optimization post up or down as they feel it's applicable. Personally, I'd prefer to read the (often interesting) meat of the discussion without seeing the same meta-discussion again and again.

It's a somewhat different story when the OP clearly doesn't understand that what they're doing is an attempt to optimize. In that case pointing out the evils of premature optimization is a reasonable response. But when the question is along the lines of "Which is faster?" the question is specifically about optimization and any discussion of premature optimization is not a reasonable response.

It's also different if you have specific information as to why this would be premature optimization. However just reciting the Knuth quotes seems silly, redundant, and not worthy of reputation.

Maybe other users don't feel the same way that I do - I just figured I'd throw it out there as I've seen it happen a few times now.

1
  • One of the first questions on Meta Stack!
    – 10 Rep
    Commented Jan 7, 2021 at 5:27

6 Answers 6

25

Premature premature optimisation discussion discussion is the root of all evil!

1
  • 6
    "Premature premature optimisation discussion discussion is the root of all evil!" is the root of all evil.
    – Michael Myers Mod
    Commented Jan 13, 2009 at 14:50
15

Please realize that a question like "Which is faster...?" is not equivalent to an attempt to actually optimize. That may be the case, but please don't assume it is, unless the OP says something to the effect of "I'm trying to make this linked-list implementation faster even though I'm only going to use it on lists with less than 10 items".

An experienced programmer who has worked on applications or libraries that scale well has a good mental model of which avenues hold the most promise for optimization, should it become necessary -- the same way most of us know how cold/warm a given temperature is, or the guy at the deli counter knows about how much a pile of cold cuts weighs before actually sticking it on the scale. So when it's time to measure performance and/or optimize, that programmer can save a lot of time by avoiding unnecessary effort.

Someone who is just starting programming in general (e.g. before understanding general algorithm tradeoffs like O(n2) bubble sort vs. O(n log n) quicksort), or who is just getting started in a particular library or system, may be trying to develop that kind of innate understanding or "wisdom" of performance & optimization techniques. Sure, it's possible to spend the time to actually put together test programs and measure it yourself, and occasionally it's good to do so, but it can save a lot of time to ask someone else who already knows the ropes. We should be pointing them at useful information about performance metrics, not just telling them that what they're doing is premature optimization.

I know I've been on both sides of the fence (both giver & receiver of performance advice) and always appreciate it when someone makes a genuine effort to provide useful assistance.

13

Any post that contains "premature optimization" should also contain a tangible, precise answer for the case that this optimization is justified.

I am sick and tired of people lecturing about premature optimization again and again. I know that this is an important lesson, but too many questions here are dismissed, which in the end isn't very helpful.

12

If you don't think the answers are helpful, vote them down. If all they do is say premature optimization is a bad thing, they're not really answering the question "which of these is faster and why?".

Answers that say "premature optimization is a bad thing, but this is the fastest way" are helpful so I would vote them up (or down if the reason is bogus).

SO is working as expected.

1
  • Agreed, it just seems like many users start their answers with "premature optimization is a bad thing", so we never really get a gauge on how applicable it is.
    – cdleary
    Commented Jan 13, 2009 at 6:59
6

The other day there was a question about which method for comparing two numbers and getting the highest numbers is the fastest. The quote holds very true here, if you're trying to optimize things like these, you're either working on an extremely small application or your optimization problems lie elsewhere.

If you have two solutions and you're wondering which is faster, just put together a small test case that actually measures which is faster. The normal answer is that it doesn't matter and lets move on. Premature optimization IS bad because what you mostly need to do optimizations are measurements of what is actually causing the application to be slow and concentrate the effort on the real slowdowns, which are quite often in areas you would not expect. Yes, there are always programmers who say that they can feel what takes time and there are also plenty of the same who are surprised when the bottleneck turns out to be something else entirely.

A comparison would probably be to a carpenter starting to cut things randomly instead of measuring first.

3

I tend to find it very annoying as well when it's unwarranted. It's an easy answer to just cite premature optimization in response to a general question that doesn't even suggest the author is even trying to optimize anything in particular.

BUT -- there are times when it is warranted, when the wise "fishing rod" answer is to suggest profiling and not obsessing over costs that might not even be there.

Sometimes the question does absolutely reek of premature optimization, like:

"Hi, I have this code I've written, didn't profile it at all ('what's a profiler?'), and I was wondering if it would be faster if I rename my variable names to be shorter so that the computer can understand them faster."

... and that's when the only decent answer is to be a parrot and talk about the importance of profiling, to become better in tune with what to optimize (and more importantly, what not to), and micro-optimizing mostly in hindsight.

It's not a very appropriate response to more theoretical/general questions asking about what could be more efficient, and even rather rude to assume the author is actually prematurely optimizing in such cases. But there are some clear cut premature optimization smells in questions (like the exaggerated one above) where this becomes the only reasonable answer.

You must log in to answer this question.