How to Improve the Execution Efficiency of Single-chip C Language Program

First of all, what is execution efficiency. The execution efficiency we usually talk about is the system overhead generated by using the same algorithm to complete the same calculation under the same input conditions. At present, more attention is paid to the execution time overhead. The code written in all languages ​​must be converted into machine code to run eventually. It is more efficient to accomplish the same thing in a shorter time.

First of all, what is execution efficiency. The execution efficiency we usually talk about is the system overhead generated by using the same algorithm to complete the same calculation under the same input conditions. At present, more attention is paid to the execution time overhead. The code written in all languages ​​must be converted into machine code to run eventually. It is more efficient to accomplish the same thing in a shorter time.

Regarding how to improve the execution efficiency of C language programs, with my years of programming experience, here I will talk about my ideas:

1. Try to avoid calling the delay function

A program without an operating system can only be executed in a loop in while(1). If a large number of delays are called in it, it will consume CPU resources. The delay is equivalent to making him stop here, only interrupts. The ones inside will be executed. If you just make a program where the LED flashes once per second, it is very simple. You can directly call the delay function. However, in actual projects, there are often many things to be done in the big loop, which is not suitable for occasions with high real-time requirements. Up. In order to avoid using a delay, you can use the timer interrupt to generate a flag bit. When the time flag bit is set to 1, you only need to check the flag bit in the main program, set it to 1, and then execute it once, and then clear the flag. Do other things at other times instead of waiting here. The best example is the Display of the digital tube, which is displayed in our routine. Then it is the button detection. The general program is done while (!key) waiting for the button to be released. If the button is kept pressed, the following program will never run and die here. In fact, you can make a button mark This problem can be avoided by detecting the falling and rising edges.

2. The code written should be as concise as possible to avoid repetition

In the book about 10 days to learn how to learn the single-chip microcomputer, I saw the part of the code displayed by the digital tube he wrote, select a bit, and then send the data, select a bit, and then send the data, and then finish. The code repetition rate is too high. It not only takes up too much class memory, but also has poor execution efficiency and poor readability. It only realizes the function. The actual programming can be a loop, for loop or while loop. Such code looks more level.

How to Improve the Execution Efficiency of Single-chip C Language Program

3. Reasonable use of macro definitions

If a variable or register is frequently used in the program, you can use the macro definition to define a new name to replace it. This has the advantage of convenient modification. For example, if the LCD data terminal bus is connected to P1, and now I want to change it to P0, then only You need to modify the macro definition here. When the compiler compiles, it will automatically replace the defined name with the actual name.

4. Use the smallest possible data type

For example, the value range of a variable is 0-255, then it is defined as unsignedchar, of course, it can also be defined as unsignedint, but this causes a waste of memory, and the efficiency of the operation is a little lower. If the data has no negative numbers, try to define it as an unsigned type. Should try to avoid being defined as floating-point data type or double-precision (occupying 8 bytes) type, these two types of operations consume CPU resources. For example, the collected voltage range is 0-5v, accurate to three decimal places, and the collected data can be expanded 1000 times, even if the maximum is only 5000, and then collect a few more times to make a filter algorithm, and finally only need to calculate the voltage Just add a decimal point after the first digit, and the variable is defined as an unsignedint variable.

5. Avoid using multiplication and division

Multiplication and division consumes CPU resources. Looking at the assembly code, you will find that a multiplication and division operation will compile a few dozen or even a few lines of code. If it is multiplied or divided by the n-th power of 2, it can be realized with >. This shift operation is already calculated at compile time, so the code is very concise and the operation efficiency is high. But you need to pay special attention to the priority of operators.

6. Try to use compound assignment operators

What is the difference between a=a+b and a+=b? The former is to first calculate the value of a+b, then save it to the ACC register, and then assign the value of the ACC register to a, while the latter directly assign the value of a+b to a, saving one step, although only one is saved Instructions, but when this calculation loops thousands of times and tens of thousands of times, then the effect is obvious. Like other -=, *=, /=, %=, etc. are the same.

7. Try not to define it as a global variable

First look at the similarities and differences of local variables, global variables, static local variables, and static global variables:

(1) Local variables: Variables defined in a function or compound statement are allocated storage units in the dynamic storage area, dynamically allocated when calling, and automatically released at the end of the function or compound statement;

(2) Static local variable: When a local variable is defined in a function, if a static declaration is added, the variable is a static local variable. The storage unit is allocated in the static storage area and will not be released during the running of the program; the static local variable is only Can be used in this function; static local variables are assigned at compile time (if no assignment is performed during definition, the default assignment is 0 (for numeric variables) or empty characters (for character variables)); static local variables are It is not automatically released after the function call ends, and the value after the function call ends is retained;

(3) Global variables: Variables defined outside the function are called global variables; global variables are allocated storage units in the static storage area, and are not released during program operation. The functions in the file can call the global variable, and in other files The function calls global variables, need to add extern declaration;

(4) Static global variables: When defining variables outside the function, if staTIc declaration is added, this variable is a static global variable; static global variables are allocated storage units in the static storage area and will not be released during program operation. Static global variables Assignment at compile time (if no assignment is performed during definition, the default assignment is 0 (for numeric variables) or empty characters (for character variables)); it can only be used in the current file.

Under normal circumstances, it is defined as a local variable, which not only runs more efficiently, but also facilitates transplantation. Most of the local variables are located in the registers inside the MCU. In most MCUs, the operation speed of the register is faster than the data memory, and the instructions are more and more flexible, which is conducive to the generation of higher quality code, and the occupation of the local variables Registers and data memory can be reused in different modules.

When the variable used in the interrupt is required, it needs to be defined as a global variable, and volaTIle is added to modify it to prevent the compiler from optimizing. If the data is read-only, such as the broken code of the nixie tube and the font library for Chinese character modulus, it needs to be placed in the ROM, so that RAM can be saved.

8. Choose the right algorithm and data structure

You should be familiar with the algorithm language and know the advantages and disadvantages of various algorithms. For specific information, please refer to the corresponding reference materials. There are many computer books that have introductions. Replacing the slower sequential search method with the faster binary search or out-of-order search method, and the insertion sort or bubble sorting method with quick sort, merge sort or root sort, can greatly improve the efficiency of program execution. .

It is also important to choose a suitable data structure. A pointer is a variable that contains an address, and the variable pointed to by it can be addressed. Using pointers can easily move from one variable to the next, so it is especially suitable for situations where a large number of variables are manipulated. Arrays and pointer statements have a very close relationship. Generally speaking, pointers are more flexible and concise, while arrays are more intuitive and easy to understand. For most compilers, the code generated by using pointers is shorter than using arrays, and the execution efficiency is higher. But in Keil, the opposite is true, the code generated by using arrays is shorter than using pointers.

9. Use conditional compilation

Under normal circumstances, when compiling a C language program, all programs participate in the compilation, but sometimes it is hoped that part of the content will be compiled only when certain conditions are met. This is conditional compilation. Conditional compilation can select different compilation scopes according to actual conditions to generate different codes.

10. Embedded compilation-killer feature

Assembly language is the most efficient computer language. In general project development, C language is generally used to develop, because the embedded assembly will affect the portability and readability of the platform, and the assembly instructions of different platforms are incompatible. But for some persistent programmers who require the program to obtain the ultimate operating efficiency, they all embed assembly in the C language, that is, “hybrid programming”.

The Links:   LQ057V3LG11 NL2432HC22-25E