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, we generally pay more attention 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.
On how to improve the execution efficiency of C language programs, there are the following suggestions:
1. Try to avoid calling delay functions
Programs without an operating system can only be executed cyclically in while(1). If a large number of delays are called here, it will consume CPU resources. The delay is equivalent to making him stop here, only interrupts. The inside will be executed. If you just make a program that the LED flashes once per second, it is very simple, you can directly call the delay function, but in actual projects, there are often many things to do in the big loop, which is not suitable for occasions with high real-time requirements. Up. In order to avoid using the 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 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 subsequent program will never run and die here. In fact, you can make a button mark This problem can be avoided by detecting falling and rising edges.
2. The code should be as concise as possible to avoid duplication
In the book of 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, then send the data, and then select a bit, then send the data, and finish in turn. 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.
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 unsigned char, of course, it can also be defined as unsigned int, but this causes a waste of memory, and the operation efficiency is 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 by 1000 times, even if the maximum is only 5000, and then collect a few more times to make a filtering 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 unsigned int 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 nth power of 2, it can be realized by using >. 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 is directly Assigning the value of a+b to a saves one step. Although only one instruction is saved, when this operation is looped thousands of times and tens of thousands of times, 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 between 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 defining a local variable 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; static local variables 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 functions are called global variables; global variables are allocated storage units in the static storage area and are not released during program operation. Functions in the file can call the global variable, and other files The function calls global variables, need to add extern declaration;
(4) Static global variable: When defining a variable outside the function, if you add a static declaration, the variable is a static global variable; static global variables are allocated storage units in the static storage area and are not 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 that of 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 a variable used in an interrupt is required, it needs to be defined as a global variable and modified with volatile to prevent compiler optimization. If the data is read-only, such as the broken code of the nixie tube and the font library for taking the Chinese character modulus, it needs to be placed in the ROM, which can save RAM. 51 single-chip microcomputers are coded, and advanced microcontrollers are modified by const.
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 with 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 can address the variable pointed to by it. 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 operated. 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 assembly---killer
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".
Note: If you want to embed assembly, you must have a deep understanding of assembly. Do not use embedded assembly if it is not a last resort.
China leading manufacturers and suppliers of SMA Cable Mount Connectors,SMA Bulkhead Mount Connectors, and we are specialize in SMA Flange Mount Connectors,SMA PCB Mount Connectors, etc.
SMA Cable Mount Connectors,SMA Bulkhead Mount Connectors,SMA Flange Mount Connectors,SMA PCB Mount Connectors
Xi'an KNT Scien-tech Co., Ltd , https://www.honorconnector.com