How to optimize C language code (required by programmers)

Source: Internet
Author: User
How to optimize C LanguageCode(ProgramMembers must read)

1. select an appropriateAlgorithmAnd Data Structure
You should be familiar with the algorithm language and know the advantages and disadvantages of various algorithms. For more information, see references.
Many books on computer science are introduced. Use the slow sequential Search Method for faster binary search or disordered search
The insert or bubble sort method can be replaced by the quick sort, merge sort, or root sort.
Improve the efficiency of program execution .. It is also important to select a suitable data structure. For example, if you store
A large number of insert and delete commands are used in the number of rows, so it is much faster to use the linked list.
Arrays have a very password relationship with pointer statements. Generally, pointers are flexible and concise, while arrays are similar
Intuitive and easy to understand. For most compilers, using pointers is shorter than using arrays to generate code,
Higher execution efficiency. In Keil, however, the code generated using arrays is shorter than that using pointers ..

3. Use data types as small as possible
If you can use a variable defined by char, do not use an integer (INT) variable.
Do not use long integer (long INT) for variables defined by integer variables. Do not use float variables.
Do not use floating point variables. Of course, after defining a variable, do not exceed the scope of the variable.
The C compiler does not report errors, but the program running result is wrong, and such errors are difficult.
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 descriptions
As for floating point parameters (% F), try not to use them. The same applies to other C compilers. If other conditions are not met
If the % F parameter is used, the number of generated codes will increase significantly and the execution speed will decrease.

4. Use auto-increment and auto-increment commands
Generally, auto-increment, auto-increment, and composite value assignment expressions (such as a-= 1 and A + = 1) can generate high-quality
Program code, the compiler can usually generate INC and DEC and other commands, and use a = a + 1 or a = A-1 and so on
Many C compilers generate two to three bytes of commands. Applicable iccavr and,
The code generated by the C compilers such as gccavr and IAR is the same and can generate high-quality
Such as INC and Dec.

5. Reduce the computing intensity
You can replace the original complex expressions with expressions that require a small amount of computing but have the same functions. 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 called
It is completed using subprograms, with long code and slow execution speed. Generally, you can obtain the remainder of 2n.
Bit operation method.

(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 of the floating point number is achieved by calling the subroutine, In the AVR Monolithic hardware Multiplier
In the machine, for example, in the atmega163, the multiplication operation can be completed in only two clock cycles. Even if there is no built-in
In the AVR Microcontroller of the hardware multiplier, the Child Program of the multiplication operation is shorter than the child program code of the square operation.
Fast.
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
Multiply by 2N to generate the Left Shift code, and multiply by other integers or divide by any number, all calls multiplication and division.
Subroutine. Using the shift method to obtain code is more efficient than calling the code generated by the multiplication and division subprograms. Actually,
If it is multiplied by or divided by an integer, you can use the shift method to obtain the result, for example:
A = A * 9
You can change it:
A = (a <3) +

6. Loop
(1) Circular Language
For tasks that do not require cyclic variables for calculation, you can put them out of the loop. Here, the task package
Including expressions, function calls, pointer operations, array access, etc., there should be no need to perform multiple operations
Put all together in an init initialization program.

(2) latency functions:
generally, the latency functions are in the form of auto-increment:
void delay (void)
{< br> unsigned int I;
for (I = 0; I <1000; I ++)
;
}< br> change it to the auto-subtraction delay function:
void delay (void)
{< br> unsigned int I;
for (I = 1000; I> 0; I --)
;< BR >}< br> the latency of the two functions is similar, however, almost all c-compiled code for the last function is 1 ~ less than the previous
code ~ Three bytes, because almost all MCU commands have 0 transfer, the latter method can
generate such commands.
the same is true when a while loop is used. Using the auto-subtraction command to control the loop will produce less code than using the self-added command to control the loop
: 1 ~ 3 letters.
when reading and writing Arrays Using the cyclic variable "I" in a loop, the use of a pre-subtraction loop may overbounds the array.

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

7. query table
Generally, complex operations are not performed in programs, such as the multiplication, division, and operations of floating point numbers.
The interpolation operation of the mathematical model, for which time-consuming and resource-consuming operations, should be used as far as possible
And place the data table in the program storage area. If it is difficult to directly generate the required table
Reduces the workload of repeated computing during program execution.

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