Monday, April 9, 2012

Universal answer about optimization

When reading through questions on Stackoverflow.com related to optimization ("is X faster than Y"), I often want to reply with the same answer, regardless of what the question was about:

When optimizing you should always follow these steps:
  • Don't optimize until you have a nice, clean, and working version of your code.
  • When you have a version which is nice and clean, decide if it is fast enough.
  • If it isn't fast enough, measure its performance. This step is important for two reasons. If you don't measure you won't (1) know the impact of any optimizations you make and (2) know where to optimize.
  • Optimize the hottest part of your code.
  • Measure again. This is just as important as measuring before. If the optimization didn't not improve things, revert it. Remember, the code without the optimization was clean, nice, and working.

No comments: