C Code Optimisation

This document has evolved over time and contains a number of the best ways to hand-optimise your C-code. Compilers are good, but they can’t do everything, and here I hope to help you squeeze the best performance out of your code. This is not intended for beginners, but for more experienced programmers.

Disclaimer

Depending on your particular hardware and compiler, some of these techniques may actually slow down your code. Do some timings with and without them, as modern compilers may well be able to do things better at a low level. Improving the overall algorithm used will often produce better results than localised code tweaking. This document was originally written as a set of personal notes for myself – do not consider it to be an authoritative paper on the subject of optimisation. I may have made mistakes! If you have anything to add to this, or just have some constructive criticism (flames ignored), please contact me at the address below.

Coding for speed 

How to squeeze those last few T-states out of your C code. This only applies where you are more concerned with maximum speed and are less concerned with readability of code.

No error-checking is shown here as this article is only concerned with the fundamentals. By the time the application gets down to the low-level routines, you should have filtered out any bad data already.

  1. Array Indices
  2. Aliases
  3. Registers
  4. Integers
  5. Loop Jamming
  6. Dynamic Loop Unrolling – can make a big difference.
  7. Faster for() loops
  8. Switch
  9. Pointers
  10. Early loop breaking
  11. Misc

Links: http://www.abarnett.demon.co.uk/tutorial.html