How to optimize C language code

Source: Internet
Author: User
Tags arrays execution expression integer pow

1, choose the appropriate algorithm and data structure

Should be familiar with the algorithm language, know the advantages and disadvantages of various algorithms, specific data see the corresponding reference materials, there are many computer books are introduced. The slower sequential lookup method is replaced by a fast binary lookup or a sequence lookup method, which can greatly improve the efficiency of program execution by using the quick sorting, merging sort or root ordering instead. It's also important to choose a suitable data structure, For example, you use a lot of insertions and deletions in a bunch of randomly stored numbers, and it's much quicker to use a linked list. Arrays and pointer statements have a very close relationship, in general, pointers are more flexible and concise, and the array is more intuitive, easy to understand. For most compilers, using pointers is shorter and more efficient than using arrays to generate code. But in Keil, in contrast, using arrays generates less code than the pointers you use.

3, use the small data type as far as possible

You can use a variable that is defined by character type (char) without using an integer (int) variable, and you can use an integer variable to define a variable that does not use a long int, and you do not use floating-point variables without using floating-point (float) variables. Of course, do not exceed the scope of the variable after the definition of variables, if the range of variables beyond the assignment, C compiler does not complain, but the program run the result is wrong, and such errors are difficult to find.

In the ICCAVR, you can set the printf parameter in options to use basic parameters (%c,%d,%x,%x,%u, and%s format specifiers) sparingly, with long integer parameters (%ld,%lu,%LX, and%LX format specifiers), as for floating-point parameters (%f) Try not to use it, the other C compilers are the same. With other conditions unchanged, using the%f parameter increases the number of generated code and slows execution.

4, the use of self-add, self-reduction instructions

It is often possible to generate High-quality program code using self-addition, self-subtraction directives and compound assignment expressions such as A-=1 and a+=1. Compilers are usually able to generate instructions such as Inc and Dec, and with instructions like a=a+1 or a=a-1, many C compilers generate two to three bytes of instruction. In the AVR monolithic application Iccavr, GCCAVR, IAR and so on the C compiler above several writes the code to generate is same, also can produce the high Quality The INC and the DEC and so on code.

5, reduce the strength of the operation

You can replace an expression that is complex by using an expression with a small amount of computation but the same functionality. As follows:

(1), residual operation.

a=a%8;

Can be changed to:

a=a&7;

Note: The bit operation can be completed with only one instruction cycle, while most of the C compiler's "%" operations are called subroutines to complete, code long, slow execution. In general, the only requirement for the remainder of the 2n is to use the bitwise manipulation method instead.

(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 the 51 series), the multiplication operation is much faster than the square calculation, 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, the multiplication operation can be done in only 2 clock cycles. Even in the AVR MCU without the built-in hardware multiplier, the subroutine of the multiplication operation is shorter than the subroutine code of the square operation, and the execution speed is fast.

If it is to ask for 3 times, such as:

A=pow (a,3.0);

Change to:

A=a*a*a;

The improvement of efficiency is more obvious.

(3), using shift to achieve multiplication and division method operation

a=a*4;

B=B/4;

Can be changed to:

a=a<<2;

b=b>>2;

Description: Usually if you need to multiply or divide by 2n, you can replace it with a shift method. In Iccavr, if multiplied by 2n, the left-shifted code can be generated, multiplied by the other integers or divided by any number, the multiplication and division procedures are called. Using the shift method to get code is more efficient than calling the code generated by the multiplication and division program. In fact, as long as it is multiplied or divided by an integer, the result can be obtained by a shift method, such as:

A=a*9

Can be changed to:

A= (a<<3) +a

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.