How to write fast code

Writing fast code requires some experience and it's hardly possible to stick to a general scheme. Nevertheless I'll provide some advice here. There are only two rules which always work and which can be considered reliable:

The golden rule of optimization

MEASURE!

One cannot emphasize this enough. When in doubt, measure. When not in doubt, measure anyway. It's impossible to predict where a bottleneck will occur. If you think you're a smartass and can, you're wrong. Measuring, on the other hand, is fast and doesn't lie. So: Measure!

The silver rule of optimization

Don't optimize!

Optimization usually results in a decrease of portability and code readability. Tiny little 0.5% optimizations usually aren't worth doing. Many people write xor eax, eax instead of the more readable mov eax, 0 for "performance reasons", even in cases where it hardly matters. On the other hand, if you can cut the speed by half somehow, do it! (To figure out how apply the golden rule.)

Some tips

Here are some tips. Before you use them, apply both the golden and the silver rule of optimization!