How to optimize C language code (programmer must Read)

Source: Internet
Author: User

How to optimize C language code (programmer must Read)5, reducing the intensity of the operation can replace the original complex expression with an expression with a small operand but with the same function. as follows: (1), to find the remainder operation. A=a%8; can be changed to: a=a&7Note: Bit operations can be done in only one instruction cycle, while most of the C compilers '%"operations are called subroutines to complete, the code is long, the execution speed is slow. In general, the only requirement is to find the remainder of the 2n side, which can be replaced by a bitwise operation. (2), square operation a=pow (A,2.0), can be changed to: a=a*A; Note: In a microcontroller with built-in hardware multiplier (such as 51 series), the multiplication operation is much faster than the square operation, because the square of the floating-point number is realized by calling the subroutine, in the AVR MCU with the hardware multiplier, such as ATMega163, Multiplication takes only 2 clock cycles to complete. Even in the AVR microcontroller without the built-in hardware multiplier, the multiplication subroutine is shorter than the sub-program code of the square operation, and the execution speed is fast. If you are seeking 3 times, such as: a=pow (A,3.0); Change to: a=a*a*A; the improvement in efficiency is more pronounced. (3), use shift to realize multiplication method operation a=a*4; b=b/4; can be changed to: a=a<<2; b=b>>2Note: Usually, if you need to multiply or divide by 2n, you can use the Shift method instead. In Iccavr, if you multiply by 2n, you can generate left-shifted code, and multiply the other integers or divide by any number, calling the multiplication method program. It is more efficient to get the code using the shift method than to invoke the code generated by the multiplication program. In fact, as long as it is multiplied or divided by an integer, you can use the displacement method to get the result, such as: a=a*9can be changed to: a= (a<<3)+a6, Loops (1), the circular language for some tasks that do not require cyclic variables to participate in the operation can put them outside the loop, where the tasks include expressions, function calls, pointer operations, array access, and so on, should not be necessary to perform a number of operations are all together, put into an init program of initialization. (3while loops and do...while loops have the following two loop forms in the while loop: unsignedinti; I=0;  while(i< +) {i++; //User Program} or: Unsignedinti; I= +;  DoI--; //User Program     while(i>0in both of these loops, the generated code is shorter than the while loop when compiled with the Do...while loop. 7, tabular in the program generally do not carry out very complex operations, such as floating-point number of multiplication and root, as well as some complex mathematical model interpolation operations, these are consumption time and consumption of resources, should try to use the way to look up the table, and the data table placed in the program storage area. If it is difficult to generate the required tables directly, it is also possible to restart, reducing the amount of repetitive computations during program execution. 

How to optimize C language code (programmer must Read)

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: 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.