More efficient C ++ Optimization

Source: Internet
Author: User

Before performing C ++ optimization, the first headache is where the C ++ code is. Before looking for the code, we should carefully study the debug-version, because 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 operator new and library functions for debug-version and release-version.

In addition, the execution body of a release-version may have been optimized in C ++ through multiple ways, including eliminating unnecessary temporary objects, expanding them cyclically, and moving objects into registers, 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:

Declaration placement
Where variables and object declarations in programs are placed will have a significant impact on performance. Similarly, selecting the postfix 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 prefix 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 advantages:

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. We have the opportunity to improve the 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.

However, for user-defined types, the two will bring a significant difference, because the 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:

 
 
  1. bool is_C_Needed();   
  2. void use()   
  3. {   
  4. C c1;   
  5. if (is_C_Needed() == false)   
  6. {   
  7. return; //c1 was not needed   
  8. }   
  9. //use c1 here   
  10. return;   
  11. }  

In the above Code, object c1 will be created even if it is possible not to be used, so that we will pay unnecessary costs for it, you may say how much time a object c1 can waste, but in this case: C c1 [1000]; I don't think it is a waste. But after C ++ optimization:

 
 
  1. void use()   
  2. {   
  3. if (is_C_Needed() == false)   
  4. {   
  5. return; //c1 was not needed   
  6. }   
  7. C c1; //moved from the block's beginning   
  8. //use c1 here   
  9. return;   
  10. }  

How is the program performance greatly improved? Therefore, please carefully analyze your code and place the Declaration in a proper position. The benefits of this Declaration are hard to imagine.

  1. How to Write C ++ project development and project plan correctly
  2. Summary Notes on learning and exploring C ++ library functions
  3. In-depth demonstration of high security of C ++
  4. Describes in detail how to accurately Write C ++ languages.
  5. In-depth demonstration of high security of C ++

Related Article

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.