premature optimization

Donald Knuth is often quoted as saying “premature optimization is the root of all evil.”

Often in the JavaRanch forums, we see questions like:
which is faster stringBuilder.append(‘a’) or “a” + “b”
(The answer is these two expressions are supposed to generate to the same byte code if used on the same line.)

My first thought when I see this is who cares. I’d be very surprised if the bottleneck in an application was being caused by this line. I’d be even more surprised if the poster had done the analysis to prove this line is the performance bottleneck before posting.

What got me thinking about this is that I think I just wrote some premature optimization on a pet project.

/* Only do search and replace if one or more images in value. This is likely premature optimization but it's faster to add the if statement than find out. */
private String convert(String value) {
  String result = value;
  if (result .contains("<img src=")) {
    for (String imageName : setOfTwentyElements()) {
      // do a regular expression search and replace
      result = result.replaceAll(imageTag,  strippedOutImage);
    }
  }
  return result;
}

The fact that I commented it means I’m aware of doing it. Which is a step ahead of what usually happens where we write the optimization assuming it will be important and later (if ever) finding out otherwise.

In this case, I expect to run the method over a million times and the value not containing an image over half the time. After that the containing code will not live on. I also know the containing code contains a database update which means the code is not the bottleneck. To me, the extra if statement seems like a low cost to avoid doing some work at all. It also feels like a code smell for premature optimization. If this were a real application and not a pet project, I would invest some time in finding out how long the code actually takes to run for my data patterns/length.

Here I’m more interested in whether it is an example of premature optimization. I think I might have to leave it a philosophical question. Comments one way or the other are certainly welcome.  My leaning is that it is still premature optimization and this post is a rationalization.