Summary of C ++ code optimization methods-general Linux technology-Linux programming and kernel information. The following is a detailed description. Optimization is a very big topic. This article does not go deep into performance analysis theories and algorithm efficiency, and I do not have this capability. I just want to summarize some optimization techniques that can be simply applied to your C ++ code. In this way, when you encounter several different programming strategies, you can estimate the performance of each policy. This is the purpose of this Article.
I. Before Optimization
Before optimization, we should first find the bottleneck of our code (bottleneck. However, when you do this, do not deduce from a debug-version because the debug-version contains a lot of additional code. A debug-version executable is 40% larger than release-version. The additional code is used to support debugging, such as symbol search. Most implementations provide different operatorew and library functions for debug-version and release-version. In addition, the execution body of a release-version may have been optimized in many ways, including the elimination of unnecessary temporary objects, loop expansion, moving objects into registers, and inline.
In addition, we need to separate debugging and optimization. They are doing different tasks. Debug-version is used to hunt down the bugs and check whether the program has logical problems. Release-version is used for performance adjustment and optimization.
Let's take a look at the code optimization technologies below:
Ii. Placement of statements
Where variables and object declarations in programs are placed will have a significant impact on performance. Similarly, the selection of ostfix and prefix operators also affects performance. In this part, we will focus on four issues: Initialize the value assignment of v. s, place the declaration where the program really wants to use, initialize the list of constructors, and refix the v. s postfix operator.
(1) Use initialization instead of value assignment.
In C, variables can only be declared at the beginning of a function body, but can be declared at any position of the program in C ++. The purpose of this operation is to delay the object declaration until it is used. This can have two benefits: 1. Ensure that the object is not maliciously modified by other parts of the program before it is used. If the object is declared at the beginning but used after 20 rows, this guarantee cannot be implemented. 2. this gives us the opportunity to improve performance by replacing the value assignment with initialization. Previously, the Declaration can only be placed at the beginning, but we often didn't get the value we wanted at the beginning, therefore, the benefits of initialization cannot be applied. But now we can initialize it directly when we get the desired value, saving one step. Note: For basic types, there may be no difference between initialization and assignment, but for user-defined types, the two will bring a significant difference, because the value assignment will call the handler function ---- operator =. Therefore, initialization should be our first choice when we select between assignment and initialization.
(2) Place the statement in a proper position
In some cases, we should pay enough attention to the performance improvement caused by moving the declaration to a proper position. For example: