C Code Optimization Scheme

Source: Internet
Author: User
Tags arithmetic arrays constant integer division nested switch pow prototype definition

C Code Optimization Scheme

1. Choosing the right algorithm and data structure

2. Use as small a data type as possible

3, reduce the intensity of the operation

(1), check table (game programmer required)

(2), to find the remainder of the operation

(3), square operation

(4), using shift to realize multiplication method operation

(5), avoid unnecessary integer division

(6), using increment and decrement operators

(7), using compound assignment expressions

(8), extract the public sub-expression

4. Layout of structure members

(1) Sort by length of data type

(2) Filling the structure into the maximum length of the whole multiple

(3) Sort local variables by length of data type

(4) Copy frequently used pointer parameters to local variables

5, Cycle optimization

(1), fully decompose the small cycle

(2). Extract the public part

(3), delay function

(4), while loops and do...while loops

(6), cyclic expansion

(6), loop nesting

(7), the switch statement according to the frequency of the case to sort

(8), convert large switch statements to nested switch statements

(9), Cycle transpose

(10), Common code block

(11) Improve the performance of the cycle

(12), select a good infinite cycle

6, improve the parallelism of the CPU

(1) using parallel code

(2) Avoid unnecessary read and write dependencies

7. Cyclic constant calculation

8. Functions

(1) Inline function

(2) Return value not used for indefinite meaning

(3) Reduce function call parameters

(4) All functions should have a prototype definition

(5) Use constant (const) whenever possible

(6) Declaring a local function static

9. Using recursion

10. Variables

(1) Register variable

(2), declaring multiple variables at the same time better than declaring variables individually

(3), short variable famous raised here variable name, should try to make the variable name a bit shorter

(4), declare the variable before the loop begins

11. Using Nested IF structures
C code Optimization Scheme 1, select the appropriate algorithm and data structure

It is important to choose a suitable data structure, which is much faster if you use a large number of insert and delete instructions in a random number of stored numbers. Arrays are very closely related to pointer statements, in general, pointers are more flexible and concise, while arrays are more intuitive and easy to understand. For most compilers, using pointers is shorter and more efficient than using arrays to generate code.

In many cases, you can use pointer arithmetic instead of an array index, which can often produce fast and short code. Pointers generally make code faster and occupy less space than array indexes. The difference is more pronounced when working with multidimensional arrays. The code below works the same, but is not as efficient.

Array index pointer operations

for (;;) {P=array

A=array[t++]; for (;;) {

a=* (p++);

。。。。。 。。。。。。

}                           }

The advantage of the pointer method is that each time an array's address is loaded with address p, only the P increment is required in each loop. In the array index method, the complex operation of the array subscript must be evaluated according to the T value in each loop. 2. Use as small a data type as possible

If you can use a variable defined by a character type (char), do not use an integer (int) variable to define it, do not use a long int for a variable defined with an integer variable, and you can avoid floating-point variables without using floating-point (float) variables. Of course, do not exceed the scope of the variable after the definition of the variable, if the scope of the variable to assign value, the C compiler does not error, but the program run the result is wrong, and such errors are difficult to find.

In Iccavr, you can set the printf parameters in the options, using the basic parameters (%c,%d,%x,%x,%u, and%s format specifiers) sparingly, with the long parameters (%ld,%lu,%LX, and%LX format specifiers), as well as floating-point parameters (%f) Try not to use it as well as the other C compilers. When other conditions are constant, using the%f parameter increases the number of generated code and slows down execution. 3, reduce the intensity of the operation (1), check the table (game programmer required)

A clever game of prawn, basically do not in their own main loop what to do arithmetic work, is definitely calculated first, then to the loop to look up the table. Look at the following example:

Old Code:

long factorial (int i)

{

if (i = = 0)

return 1;

Else

return I * factorial (i-1);

}

New Code:

static long factorial_table[] =

{1, 1, 2, 6, +, 720 * * etc * *};

long factorial (int i)

{

return factorial_table[i];

}

If the table is large and hard to write, write an init function that temporarily generates a table outside of the loop. (2), to find the remainder of the operation

a=a%8;

Can be changed to:

a=a&7;

Note: bit operations need only a single instruction cycle to complete, and most of the C compiler's "%" operation is called subroutines to complete, long code, slow execution. In general, the only requirement is to find the remainder of the 2n side, which can be replaced by a bitwise operation. (3), 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), 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 microcontroller with the hardware multiplier, such as ATMega163, multiplication operation can be done in only 2 clock cycles. 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 the request is 3 times, such as:

A=pow (a,3. 0);

Change to:

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.