First, what is execution efficiency? The execution efficiency we usually refer to is the system overhead generated by using the same algorithm to complete the same calculation under the same input conditions. At present, generally, more attention will be paid to the execution time overhead. Code written in all languages ​​will eventually run and must be converted into machine code. It is more efficient to accomplish the same thing in less time.
Regarding how to improve the execution efficiency of the C language program, there are the following suggestions:
1. Try to avoid calling delay functions
A program without an operating system can only be executed cyclically in while (1). If you call a large amount of delay in it, it will consume CPU resources. The delay is equivalent to letting him rest here, only interrupting Only the inside will be executed. If you just do a program that LED flashes once a second, it is very simple, you can directly call the delay function, but in actual projects often have a lot of things to do in the big loop, not for occasions with high real-time requirements Too. In order to avoid the use of delay, you can use the timer interrupt to generate a flag bit. When the time flag bit is set, you only need to detect the flag bit in the main program. When it is set, it is executed once, and then the flag is cleared. I will do other things at other times instead of waiting here. The best example is the display of the digital tube, using the interrupt key display, in our routine. Then it is the key detection. The general program is to do while (! Key) to wait for the key to be released. If the key is kept pressed, the subsequent program will never get run and die here. In fact, you can make a key logo This problem can be avoided by detecting falling and rising edges.
2. Write the code as concisely as possible to avoid duplication
In the book that learned the MCU in 10 days, he saw the part of the code displayed by the digital tube he wrote, select a bit, and then send the data, then select a bit, and then send the data, and then finish. The code repetition rate is too high, which not only takes up too much class memory, but also has poor execution efficiency and poor readability. It only implements 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 a macro definition to define a new name instead of him. This has the advantage of being easy to modify. For example, P1 connected to the data terminal bus of the LCD, and now you want to change 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 does not have a negative number, try to define it as an unsigned type. You should try to avoid being defined as a floating-point data type or a double-precision (8 bytes) type. These two types consume a lot of CPU resources when computing. For example, the collection voltage range is 0-5v, accurate to three decimal places, you can expand the collected data by 1000 times, even if the maximum is only 5000, then collect several times to make a filtering algorithm, and finally only need to calculate the voltage It is enough to add a decimal point after the first digit, and the variable is defined as an unsigned int variable.
5. Avoid 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 10 or even dozens of lines of code. If it is multiplied or divided by 2 to the nth power, you can use> to achieve this kind of shift operation is already calculated at compile time, so the code is very simple 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 calculates the value of a + b first, then saves it to the ACC register, and then assigns 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, but when this operation loop is 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 global variables
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 called, 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 the static statement is added, the variable is a static local variable, and the storage unit is allocated in the static storage area, and is not released during the program operation; the static local variable is only Can be used in this function; static local variables are assigned at compile time (if no assignment is made at the time of definition, the default assignment is 0 (for numeric variables) or null characters (for character variables)); static local variables are The function is not automatically released after the end of the function call, and the value after the end of the function call 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 the running of the program. Functions in the file can call the global variable, and other files Function calls global variables, need to add extern declaration;
(4) Static global variable: When a variable is defined outside the function, if the static statement is added, the variable is a static global variable; the static global variable is allocated a storage unit in the static storage area and is not released during the program operation, the static global variable Assignment at compile time (if no assignment is made at the time of definition, the default assignment is 0 (for numeric variables) or null 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 is easy to transplant. Local variables are mostly located in registers inside the MCU. In most MCUs, the operation speed of using registers is faster than the data memory, and the instructions are more and more flexible, which is conducive to generating higher-quality code, and the occupation of local variables Registers and data memory can be reused in different modules.
When the variables that need to be used in the interrupt, they need to be defined as global variables and modified with volatile to prevent the compiler from optimizing. If the data is read-only, such as the broken code of the digital tube, the Chinese character modulo font library needs to be placed in the ROM, so that RAM can be saved, the 51 single-chip is added with code, and the advanced single-chip is added with const modification.
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, which are introduced in many computer books. Replacing the slower sequential search method with a faster binary search or out-of-order search method, and inserting or bubble sorting 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 containing an address, and the variable pointed to by him can be addressed. Using pointers can easily move from one variable to the next, so it is especially suitable for the operation of a large number of variables. 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 use of pointers is shorter than the code generated using arrays, and the execution efficiency is higher. In Keil, on the contrary, the code generated using an array is shorter than the pointer used.
9. Use conditional compilation
In general, when compiling a C language program, all programs participate in the compilation, but sometimes it is hoped that some 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 the actual situation, thereby generating different codes.
10. Embedding assembly --- killer
Assembly language is the most efficient computer language. C language is generally used in the development of general projects, because embedding 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 achieve extreme 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 unless it is absolutely necessary.
If you are looking for super quality OEM brand new laptop charger at the most preferred prices, you just come to the right place. We are professional manufacturer to provide the high quality laptop ac Adapter with factory competitive prices. Our products range include replacement ac adapter for all brands such as apple ,dell, hp/Compaq, Microsoft ,IBM/Lenovo, Sony, Toshiba, Fujitsu, Gateway, Liteon, Delta, NEC, LS, LG and so on.
With many years of market experience, we have gained the trust of a wide audience. All our laptop adapters have passed CE FCC ROHS UL CUL ETL CB certificates with full 1 year and 2 years warranty. Replacement ac adapter with smart circuit keeps your laptop safe with multiple protections, including over current protection, over voltage protection, short circuit protection, over heating protection and overload protection.
If you are not sure the laptop Power Supply specifications you need, please tell us your laptop model or your original adapter part number, we will help you select the correct laptop adapter for use, OEM&ODM orders are welcome, we can add your brand on products or package. If you have any questions about notebook adapter, please be free to contact us directly, we will be for you with 24 hours ASAP.
Laptop Charger,Computer Charger,Laptop Battery Charger,Notebook Charger
Shenzhen Yidashun Technology Co., Ltd. , https://www.ydsadapter.com