Common optimization strategies for C + + code
1. There is no reference to a null value, which means that the reference is more efficient than a pointer, because it is not necessary to test its legitimacy before using the reference; The pointer can be re-assigned to point to a different object, but the reference always points to the object that was specified when it was initialized.
2. Use bit operation instead of subtraction to calculate the residual.
3. Switch instead of multilayer if else, see the assembly code, switch a bit of space to change the meaning of time. If switch is not a row, place the most common scene at the front of the If branch chain.
4. Reduce function calls, function calls require two jumps, plus stack memory operations.
5. Using inline functions to deal with small code-size functions to eliminate function call overhead, space-time, actual work, if the amount of code is not very large, I will generally use forced inline __attribute__ ((always_inline)).
6. To prevent shallow copying, for example, there is a simple class, preferably simple (), simple (const simple&), simple& operator= (const simple&) declared as private, or inherit boost from the Noncopyable library, class Simple:public boost::noncopyable{}.
7. Reduce the use of temporary objects.
8.a+=b form efficiency is higher than a=a+b, so use + =,-= ,*=, and/= instead of + , - , * , /.
9. Optimize at the cache level (for example, to make a structure fill a cache line, increase the hit rate, etc., there is a lot of content here, the opportunity to analyze later).
10. If possible, put the object on the stack as much as possible, do not put on the heap, that is, when initializing the use of a A (variable 1, variable 2, ...) Instead of a A = new (variable 1, variable 2, ...).
11. Try initializing the initialization list for example:a::a (): A (0), B (0), C (0) {}, instead of the initialization function a::a () {a= b = c = 0;}.
12. If the function does not require a return value, do not define it.
13. Minimize the amount of computation you have in your program, such as simplifying your calculations on draft paper.
14. Unwanted data, do not initialize, initialize large chunks of memory, use Memset.
15. Consider using profiling tools such as profiler and VTune to identify time-consuming parts of the program that are accurately positioned to optimize the part.
16. When the For statement loops the increment variable, use ++i instead of the i++, because the former does not need to return a temporary object.
17. Reduce the memory copy operation, reduce the use of loops and recursion, can be replaced with a pointer to the absolute non-copy to pass the whole block of memory.
The 18.vector clear method does not reclaim memory and uses swap to reclaim memory.
19. When using an STL container, create a container for the pointer instead of the object's container, because the copy pointer is fast.
20. When passing parameters in the function, if it is a large object (vector, etc.) as far as possible to pass the reference.
C + + Common code optimization strategy