Optimization, Step By Step

A week ago I wrote how optimization on legacy code can yield great benefits. But how do you go about optimizing this code? First of all a couple of well known guidelines:

  • Don’t optimize too early in the development cycle (until and unless you are writing code for the GPU :)). Get the functionality perfect before you start optimization.
  • The best optimizations are obtained by replacing an inefficient algorithm with an efficient one. Before you start code optimization ensure that you are using the best possible algorithms.

Given that the above two points have been more-or-less followed during the development process, here is a quick step-by-step approach towards code optimization:

  1. Profile the code. Use a third-party tool, if it works for you. Better yet, try to have your own profiler built into the code.
  2. If there are a few functions that take an order of magnitude more time than the rest, they are obvious targets for optimization. Optimizing anything else doesn’t matter much.
  3. If these functions are calling third-party APIs, check if you can reduce the number of calls.
  4. If you are computing anything repeatedly, check if you can cache that data in case it is expensive to compute.
  5. Check you are using the programming language features in a way that enhances performance.
  6. Try using compact data-structures and strive for cache-coherency.
  7. Hand-code a few functions to use advanced processor instructions or code bits and pieces in assembly.

Post a Comment

Your email is never shared. Required fields are marked *

*
*