Sunday, November 27, 2011

When should you take performance into consideration?

Before publishing my previous post on rewriting an if, I knew some people would hate it, because the refactored construct is less performant.

Although I think performance is important, relevant performance improvements are, apart from in tight loops, hardly ever to find in language constructs. To put it more bluntly, they are a waste of time. When translating your thoughts into code, you should aim to make your intentions as clear as possible for the person who comes after you. Don't obfuscate your code for an negligible performance improvement.

I'm not advocating readability and pretty code are an end goal, performance and quality still are. But look for optimizations where it counts: optimizations which, after measuring, prove to be make a significant impact on the overall performance of your system. These are most likely to be found in I/O operations, architectures and algorithms. Not in language constructs.


  1. If your rewrited if code would be located in an iteration with a size of 1K+ it would be worth the fine tuning.

    But i aggree, don't worry about that untill you hit that point.

    Making your code readable and adjustable is more important than making it performant, you can always do that later.

  2. I agree with your post. Performance should not be the first consideration when writing code. Of course we can write code to avoid obvious performance problems, like avoid to loop through a list twice to find duplicates, but for most code we write, we should focus on writing clear and concise code, then come back to optimize it later if performance goals are not reached.