Summarize c++ performance optimization strategy
- 2020-06-03 07:47:13
1 about inheritance: there is no denying that a good abstract design more clear, your program can code more looks better, but she is also a loss, in succession system the subclass will create calls the superclass constructor, destroyed when calling the superclass destructor, the consumption will rise with the depth of inheritance in a straight line, so don't be too abstract and inheritance.
Composition of 2 objects: Composition of objects is similar to inheritance, and additional construction occurs when an object contains other object constructs. For example, if you use arrays and strings in an object, do you choose string and vector or char * and c arrays? If you do not use the advanced usage provided by the c++stl library, you are advised to choose the latter.
Constructor: Try to substitute parameter list initialization for parameter to avoid value passing initialization.
4 variable delay definition: from c department has kept the habit of c turned, in the first line of the function to define all variables used is good, but there is no runtime c consumption, for when c + + 1 sample, for c + + object construction and destruction of consumption, if there is a lot of objects only appear in a branch of a if conditions, there will be 50% of the time these costs can be avoided. This is also true for 1 class, if any member is available only at a certain point, use a pointer instead, initialize the object to a null pointer during construction, and avoid calling its constructor during construction.
5 virtual functions: the underlying implementation of virtual functions is implemented through a virtual function table, so when the class structure has a virtual function must first initialize the virtual function table, function call also have to find a virtual function table, and then find the corresponding function through the pointer offset, and if the existence of virtual inheritance will further growth in this process, it is the consumption of runtime, so to avoid abuse of virtual functions and virtual inheritance, as far as possible with the template design instead of a virtual inheritance runtime advance consumption to compile time.
6 return value optimization: although c + + compiler will selectively optimize RVO but not mandatory, when the function has more than one return statement and return to the name of the object, the function is too complex, the returned object is not defined copy constructor, rvo optimization will not perform, so when the function returns a large object in uncertain rvo optimization is executed, avoid value.
Definition of variables: Try to avoid temporary variables caused by type mismatch when defining variables.
8 Memory management: c++ memory management is in our own hands. It is suggested to use a simple memory pool to manage the objects to be frequently applied and released in the project, which can greatly reduce the consumption caused by frequent application and released memory.
9 use inline: inline function, optimization is not only a simple function call, which he still has a biggest advantage is that can let the compile-time boundary code running environment optimization, inline code copy to the execution environment to avoid the consumption of function calls, and compile time can normal compiler optimization, and the function call is not implemented.
10 stl: Remember that 1 point stl is not the only choice, and sometimes it is not the best choice. Choose stl reasonably and make good use of stl algorithm.
Cache: Cache the results of multiple USES in a timely manner to avoid double-counting.
Delay calculation: try to delay or asynchronously execute the calculation process that does not care about the calculation result.
13 multithreading: as far as possible the use of unlocked multithreading development, lock is a very cost performance of things, to ensure that there are many means of data synchronization, voalite, atomic operations can be achieved, if forced to reduce the consumption of the lock, such as reducing the granularity of the lock, the use of higher performance of the lock and so on.
14 cpu cache: Using cpu cache properly can greatly improve the efficiency of code running, such as the difference between traversing an array by column and by row. Of course, we should also consider the impact of multithreaded environment.
Memory alignment: During network programming, it is better to make up the memory for the fast data transmitted in the network to speed up the reading speed of network data.
16 Function parameters: const references are used instead of value passing. If the function parameters are too many, objects can be used to package the parameters to reduce the performance cost caused by too many parameters.
Algorithm 17: Optimize your algorithm as much as possible.
18 other optimization schemes: bit operation instead of multiplication and division, prefix operator instead of suffix operation, etc.