Coding and Optimization Techniques

Code size and Code speed.

Some optimziations improve one at the expense of the other, but some techniques can improve both.

Code optimizations happen at a variety of levels, starting at the highest level:

    • Algorithm and Problem Formulation
    • High level (intermediate code) optimizations
    • Low level (architecture dependent) optimizations

Most of us know that algorithmic optimizations often provide the biggest improvements.

  1. Check closely for unused variables/constants/instructions/subroutines, as that can consume space.
  2. Another thing that can be done is to aggressively look for repeated stretches of code and gather them into subroutines, rather than coding them in line. This also improves cache performance.
  3. Another technique that works if you have null terminated strings (ala C/C++) is to look for constant strings where one string is a suffix of another string is to express the shorter suffix by creating a pointer into the correct position in the longer string.
  4. Use the stack when possible, this tends to reduce memory usage, since variables are only memory resident when they are in scope (and are likely to be used).
  5. Be careful about register allocation, don't have unused registers at the expense of memory.