C++ code optimization

2 02 2009


These are the some tips to improve the performance of the software you developed.

1. Minimize local variables.

2. Declare local variables in the inner most scope.

3. Reduce number of function parameters.

4. Use reference parameters instead of pass by value.

5. Don’t define a return value if it’s not used.

6. Prefer integer over char and short while performing arithmetic operations.

7. Prefer initializtion over assignment.

8. Use constructor initialization list.

9. Convert small functions into inline functions.

10. Avoid virtual functions if possible.

11. Use pre-increment operators instead of post-increment operators.

12. Try to declare the variables outside the loop.

13. It is better to set the size of the vector if you know it.

14. If an object contains several integer numbers with a small range, transform them in bit-fields.

15. Do not invoke virtual functions in constructors and destructors.

16. Avoid using declaration.

17. Use STL containers as the first choice.

18. Avoid code duplication.

19. Try to use auto pointers instead of normal pointers.

20. Use forward declaration when possible.

21. Practice Interface based programming.




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: