How to get started optimizing your programs

From ArticleWorld

Program optimization is one of the subjects on which countless books, articles and documents have been written. However, many are missing the starting points required for a beginner to get started.

The problem is, most of them explain strategies for profiling or improving the algorithm complexity, without explaining where each of these pieces go into the puzzle or what you should learn. This guide tries to give a panorama of this, explaining the basic things to learn.

How to go about optimization

  1. The starting point of everything is the running time of the algorithm, the well-known "O". If you are serious about optimizing your algorithms, you should be able to find the approximate running time of one. This will make it easier for you to estimate how much optimization can be done.
  1. Know the underlying technology. Some architectures can perform certain calculation types easier than others, and this should give you hints about how to simplify your calculation. For example, multiplication is typically performed faster than division. The same goes for many other constructs, too.
  1. Try to understand exactly when and how to use an algorithm. The classic example is that of Fibonacci numbers. If what you are trying to do is compute the nth Fibonacci number, you will simply use the formula described (or re-discovered) by Dijkstra, while computing the first n Fibonacci numbers will require you to use Fibonacci's formula.
  1. Learn multithreading. This is one of the most important assets in some cases.
  1. Learn assembly language. This will allow you to make a lot of clever optimizations at times, but do expect it to be difficult to learn and use productively in a short period of time. If the compiler and language you use, use in-line ASM code when possible.
  1. Learn to profile your code. There are tools for doing this, allowing you to understand what parts of your code are slowest and need optimization.
  1. Think for yourself. Some say that, at today's hardware level, there is no need for software optimization, but this is false. The same goes for optimizing compilers: examine the generated code by yourself. There is always room for optimization there.