Saturday 30 October 2010

Premature Optimisation is the Root of all Evil

It's hard to engage in any discussion about performance without someone bringing up the oft-quoted line of Knuth's:
“Premature optimization is the root of all evil.” - Donald Knuth
Now there is good advice in there, but it appears many programmers unfurl this phrase to emphesise a need to not concern yourself with performance. The issue is that there are several kinds of optimisation.

  • Optimisation from good design
  • Optimisation from good coding practices
  • Optimisation from measuring

Only the last of these could be considered a "premature optimisation" if you do it too early, but the others are fundamental to writing good code. Maintaining good clean coding practices is something every programmer should concentrate on for every line of code that they write and good design is something that happens before you even touch an IDE.

You should never forsake readability for performance, but if you are not able to improve the speed of your code while maintaining legibility, that would indicate a problem with either your design or your algorithms.

No comments:

Post a Comment