Optimize C language code (required by programmers)

Source: Internet
Author: User

1. select an appropriate algorithm and Data Structure
You should be familiar with the algorithm language and know the advantages and disadvantages of various algorithms. For more information, see relevant references. Many computer books have introduced this. The slow sequential search method is replaced by the faster binary search or disordered search method, and the insert or bubble sort method is replaced by the quick sort, merge sort, or root sort, can greatly improve the efficiency of program execution .. It is also important to select a suitable data structure. For example, if you use a large number of insert and delete commands in a pile of Random storage numbers, it is much faster to use the linked list. Arrays are closely related to pointer statements. Generally, pointers are flexible and concise, while arrays are intuitive and easy to understand. For most compilers, using pointers is less efficient than using arrays to generate code. In Keil, however, the code generated using arrays is shorter than that using pointers ..

2. Use data types as small as possible
If you can use a variable defined by char, do not use an integer (INT) variable. If you can use an integer variable to define a variable, do not use a long integer (long INT ), do not use float variables without using float variables. Of course, after defining a variable, do not exceed the scope of the variable. If the value is greater than the scope of the variable, the C compiler does not report an error, but the program running result is wrong, and such errors are hard to be found.
In iccavr, you can set the printf parameter in options and try to use basic parameters (% C, % d, % x, % x, % u, and % s format specifiers ), use less long integer parameters (% lD, % lu, % lx, and % lx format specifiers). Do not use floating point parameters (% F). Do not use other C compilers. Using the % F parameter without changing other conditions will increase the number of generated codes and reduce the execution speed.

3. Use auto-increment and auto-increment commands
Generally, high quality program code can be generated using auto-increment, auto-increment, and composite value assignment expressions (such as a-= 1 and A + = 1, the compiler can usually generate commands like INC and Dec, and use commands like a = a + 1 or a = A-1, many C compilers generate two to three bytes of commands. The code generated by the above writing methods is the same for the c compilers such as iccavr, gccavr, and IAR, which can also generate high-quality Code such as INC and Dec.

4. Reduce the computing intensity
It can be used for a small amount of computing,FunctionReplace the original complex expressions with the same expressions. As follows:
(1) perform the remainder operation.
A = A % 8;
You can change it:
A = A & 7;

Note: Bit operations can be completed in only one instruction cycle, while most of the C compiler's "%" operations are completed by calling subprograms. The code is long and the execution speed is slow. Generally, bitwise operations can be used to obtain the remainder of 2n.

(2) Square Calculation
A = POW (A, 2.0 );
You can change it:
A = A *;

Note: In a single-chip microcomputer with built-in hardware multiplier (such as 51 series), multiplication is much faster than square calculation, because the square Calculation of floating point numbers is achieved by calling subprograms, in an AVR microcontroller that comes with a hardware multiplier, for example, in atmega163, the multiplication operation can be completed with only two clock cycles. Even in an AVR Microcontroller without a built-in hardware multiplier, the subprogram of multiplication is shorter than the subprogram code of the square operation, and the execution speed is faster.

If the power is 3, for example:
A = POW (A, 3.0 );
Changed:
A = A * a *;
The efficiency improvement is more obvious.

(3) shift to realize multiplication and division
A = A * 4;
B = B/4;
You can change it:
A = A <2;
B = B> 2;

Note: If you need to multiply or divide by 2n, you can use the shift method instead. In iccavr, if it is multiplied by 2n, the code for left shifting can be generated. If it is multiplied by other integers or divided by any number, the multiplication and division subprograms are called. Using the shift method to obtain code is more efficient than calling the code generated by the multiplication and division subprograms. In fact, if it is multiplied by or divided by an integer, the result can be obtained by shift, for example:
A = A * 9
You can change it:
A = (a <3) +

5. Loop
(1) Circular Language
For tasks that do not require cyclic variables for calculation, you can put them out of the loop. The tasks here include expressions, function calls, pointer operations, and array access, put all the operations that are not necessary to be executed multiple times together in an init initialization program.

(2) latency functions:
The commonly used latency functions are in the form of auto-increment:
Void delay (void)
{
Unsigned int I;
For (I = 0; I <1000; I ++)
;
}
Change it to the auto-subtraction delay function:
Void delay (void)
{
Unsigned int I;
For (I = 1000; I> 0; I --)
;
}

The latency of the two functions is similar, but almost all c-compiled functions generate less code than the previous one ~ Three bytes, because almost all MCU commands have 0 transfer, the latter method can be used to generate such commands. The same is true when a while loop is used. Using the auto-increment command to control the loop will produce less code than using the self-increment command to control the loop ~ 3 letters. However, when reading and writing an array using the cyclic variable "I" in a loop, using a pre-subtraction loop may cause the array to be out of bounds.

(3) While loop and do... While Loop
When using a while loop, there are two types of loops:
Unsigned int I;
I = 0;
While (I <1000)
{
I ++;
// User program
}
Or:
Unsigned int I;
I = 1000;
Do
I --;
// User program
While (I> 0 );
In these two loops, do... The code generated after the while loop compilation is shorter than the while loop.

6. query table
In a program, it generally does not perform very complex operations, such as floating point multiplication and division, and some complex mathematical model interpolation operations, which consume time and resources, table store should be used as much as possible, and data tables should be placed in the program storage area. If it is difficult to directly generate the required table, we try to start it as much as possible, reducing the workload of Repeated Computation during program execution.

7. Others
For example, online assembly and saving strings and some constants in program memory are conducive to optimization.

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.