What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 140 Stack Exchange communities.

Several of the questions on SO that ask, "Which of these is faster?" or "Why is this faster than the alternative?" have one or more answers which explain the downsides of premature optimization and suggest that 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 ;-)

Maybe we could 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? We'd be centralizing our information a bit (DRY) and people could vote it up or down as they feel it's applicable. Other than that, 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, but when the question is along the lines of "Which is faster?" they at least have some idea what they're talking about. It's also different if you have specific information as to why this would be premature optimization, but 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. I've seen it happen a few times now.

share|improve this question

migrated from stackoverflow.com Mar 29 '10 at 22:55

This question came from our site for professional and enthusiast programmers.

6 Answers 6

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

share|improve this answer
+1 for being a funny b*gger. –  paxdiablo Jan 13 '09 at 7:27
"Premature premature optimisation discussion discussion is the root of all evil!" is the root of all evil. –  mmyers Jan 13 '09 at 14:50

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.

share|improve this answer

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.

share|improve this answer
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 Jan 13 '09 at 6:59

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.

share|improve this answer

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.

share|improve this answer

I'd be OK with 'Which is faster?' questions if the OP mentions that they have profiled the code and it is the bottleneck.

A bonus with putting this in the question is:

  • It gives background to the question to make it more relevant, not just some trivial academic question that has no relevance to the real world. I'd rather spend my time helping people to solve real problems. (I enjoy playing around with trivial issues too, but I'd like to know that upfront before I decide to invest my time in it).
  • It shows that the OP has made an effort.
  • Anyone else who finds the question will immediately see the good advice to performance profile without having to scroll down to find it amongst the answers (many people don't read low voted answers and would miss this vital piece of information).
  • It saves people having to waste time making the same reference to Knuth so that they instead can spend their time answering the question.

If the question does not mention profiling then IMHO an answer that also doesn't mention it is an incomplete answer. Premature optimization is probably the single biggest disease in our industry (closely followed by trying to using regular expressions to parse HTML). The only way to cure this disease is through education. I feel that this site is in a unique position to do that so this opportunity should not be wasted.

share|improve this answer

You must log in to answer this question.