Rule one of optimizing: Find a better algorithm.
I disagree there. Rule one of optimising: DON'T!
at least, not until you have a working program which has been thoroughly tested and the result of that testing has shown that it actually needs optimising; at which point, careful analysis needs to be done to make sure that the bits which are being examined for optimisation are actually the bits which are causing problems in the first place.