Several techniques for optimizing C and C++ code are covered:
- Adjust structure sizes to power of two
- Place case labels in narrow range
- Place frequent case labels first
- Break big switch statements into nested switches
- Minimize local variables
- Declare local variables in the inner most scope
- Reduce the number of parameters
- Use references for parameter passing and return value for types bigger than 4 bytes
- Don’t define a return value if not used
- Consider locality of reference for code and data
- Prefer int over char and short
- Define lightweight constructors
- Prefer initialization over assignment
- Use constructor initialization lists
- Do not declare “just in case” virtual functions
- In-line 1 to 3 line functions
Advertisements