The first step in optimization is to determine which problems you have. Size issues or speed issues? Let the compiler do the optimization first. In GNU C, O3 turns on all available gcc optimizations.
To increase code efficiency, you may try:
- inline function.
- assembly. Usually compiler will do this, but sometimes hand-coded assembly is better.
- register variables.
- global variables. Sometimes it is better than passing parameters to functions.
- fix-point arithmetic. Try to avoid float.
- check you loop. Use loop unrolling.
To decrease code size:
- avoid standard library routines, the function like ‘strupr…’ may cost your code.
- use ‘goto’ statement if it is necessary.