A detailed description of the C ++ optimization code

Source: Internet
Author: User

C ++ also supports process-oriented programming. Instead of a pure object-oriented language, it maintains C's conciseness, efficiency, and closeness to assembly language, the class mechanism was introduced. The original C ++ was called "C with Class". Now, let's start to explain the C ++ optimization code.

To make the compiler generate better C ++ optimization Code (for example, generate 3 DNow! Or SSE command code), you must make sure that the floating point variables and expressions are float type. Note that the floating point constant with the suffix "; F"; or "; f"; for example, 3.14f) is the float type. Otherwise, the double type is used by default. To avoid the float type parameter being automatically converted to double, use float in function declaration.

32-Bit Data Type

There are many compilers, but they all contain the typical 32-bit types: int, signed, signed int, unsigned, unsigned int, long, signed long, long int, signed long int, unsigned long, unsigned long int. Try to use a 32-bit data type, because they are more efficient than 16-bit data or even 8-bit data.

  • C ++ standard library description
  • In-depth analysis of C ++ software programming skills
  • Technical support for C ++ code editing and skills
  • Notes for analyzing C ++ Development
  • Describes how to build a Visual C ++ Environment

Wise to use signed integer variables

In many cases, you need to consider whether integer variables are signed or unsigned. For example, when saving a person's weight data, a negative number is not allowed, so you do not need to use a signed type. However, to store temperature data, you must use signed variables.

In many cases, it is necessary to consider whether to use signed variables. In some cases, signed operations are faster, but in some cases, the opposite is true. For example, when an integer is converted to a floating point, it is faster to use a signed integer larger than 16 bits. Because the x86 architecture provides instructions for converting signed integer data to floating point data, but it does not provide instructions for converting unsigned integer data to floating point data. Look at the compilation code generated by the compiler. Bad code:

 
 
  1. double x; mov [foo + 4], 0   
  2.  
  3. unsigned int i; mov eax, i   
  4.  
  5. x = i; mov [foo], eax   
  6.  
  7. flid qword ptr [foo]   
  8.  
  9. fstp qword ptr [x]   

The above code is slow. The execution of FLID commands is delayed not only because of the large number of commands, but also because the command cannot be paired. We recommend that you use the following C ++ optimization code instead:

 
 
  1. double x; mov [foo + 4], 0   
  2.  
  3. unsigned int i; mov eax, i   
  4.  
  5. x = i; mov [foo], eax   
  6.  
  7. flid qword ptr [foo]   
  8.  
  9. fstp qword ptr [x]   

When using the unsigned type to calculate the quotient and remainder in integer operations, it is faster. The following typical code is the code generated by the compiler for dividing the 32-bit integer number by 4. It is recommended for poor code:

 
 
  1. int i; mov eax, i   
  2.  
  3. ii = i / 4; cdq   
  4.  
  5. and edx, 3   
  6.  
  7. add eax, edx   
  8.  
  9. sar eax, 2   
  10.  
  11. mov i, eax  

In programming, we often need to use infinite loops. The two commonly used methods are while (1) and (;;). The two methods have the same effect, but what is better? Let's take a look at the compiled C ++ optimization code:

 
 
  1. int i; mov eax, i   
  2.  
  3. ii = i / 4; cdq   
  4.  
  5. and edx, 3   
  6.  
  7. add eax, edx   
  8.  
  9. sar eax, 2   
  10.  
  11. mov i, eax  

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.