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.

3 thoughts on “premature optimization

  1. Finally…someone who gets it. I can’t count how many blog entries about which is faster…this or that…Then they show that if you do the same line of code 10 million times, one is one second faster. Who cares? If an end user won’t notice the difference, it isn’t worth changing. In my example, even if you did run that line of code 10 million times for a single request from a user, it isn’t worth changing.

  2. I’d always argue that pre-optimization is evil (and I also agree with the commenter above). However in your case is it not a piece of ‘business’ logic? e.g. You only want to convert images, I could see the argument for having the method name convertImage (or some such)

  3. Martijn,
    I suppose it could be considered business logic. Technically all the images are of the form So running the logic regardless would be just as accurate. I like your rationalization that it is explanatory though.

Leave a Reply

Your email address will not be published. Required fields are marked *