Premature Optimization and Bugs

All developers are aware of Knuth’s warning about the dangers of premature optimization. Yesterday, I ran across two posts that nicely illustrate those dangers in the wild.

The first is a tweet that explains why Unix files that begin with a dot are hidden—that is, why they are not normally displayed by the ls command. That turns out to be a bug: the result of an optimization attempting to simplify a test for another condition.

The second is a post by Eric Raymond that explains how he was recently seduced by the siren song of premature optimization. He ended up with buggy, complicated code that, as it turns out, was slower than doing the same task by the obvious brute force approach.

If you write code, you should definitely read these two items. They serve as an excellent reminder that even the masters can get tripped up when they optimize too early. That said, I’m sure I haven’t done it for the last time but at least we’re all warned.

UPDATE: ESR wrote to say that he hadn’t measured the relative speeds of the two methods so I’ve deleted that part of the post.

This entry was posted in Programming and tagged . Bookmark the permalink.