Summary of the methods for optimizing C ++ code

Source: Internet
Author: User
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:
  
  
QUOTE:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.