Career thoughts

Monday, January 02, 2006

C/C++ optimization tips

http://www.eventhelix.com/RealtimeMantra/Basics/OptimizingCAndCPPCode.htm

Read an interesting article containing some pointers for better performance coding.

1. Structure sizes should be powers of two ( easy to address arrays in those cases ).
2. Switch/case optimizations : Narrow range for switch labels.
3. Minimize local variables to avoid operations on the frame pointer. If everything fits in the register, thats great !
4. Declare local variables in the inner most scope.
5. Reduce number and size of parameters. Use const references to pass bigger structures.
6. Locality of reference for code and data.
7. Inline functions for 2-3 line functions.
8. Prefer int over char/short. Compiler converts it to int anyway.
9. Prefer inititalization over assignment. And prefer lightweight constructors.
10. Use Constructor Initializaition Lists instead of assigning in constructur body.
11. Minimize Virtual Functions



Note that c and c++ push function parameters in reverse ( right to left order ).

0 Comments:

Post a Comment

<< Home