Optimizing a program usually refers to optimizing program code or program execution speed. Optimizing code and optimizing speed are actually the same thing. Generally, if the size of the code is optimized, the execution time will increase. If the execution speed of the program is optimized, the code will increase. It is difficult to have both. You can only find a balance when designing. 1.
Optimizing the structure
of the program 1.
Although the writing format does not affect the quality of the generated code, you should still follow certain writing rules when writing programs. A clear and concise program is conducive to future maintenance. When writing programs, especially for statements such as While, for, do...while, if...elst, switch...case, or nested combinations of these statements, you should use the "indented" writing form.
2.
In addition to following the naming rules of identifiers, user identifiers used in programs generally do not use algebraic symbols (such as a, b, x1, y1) as variable names. English words (or abbreviations) or Chinese pinyin with relevant meanings should be selected as identifiers to increase the readability of the program, such as: count, number1, red, work, etc.
3. Program structure
C language is a high-level programming language that provides a very complete standardized process control structure. Therefore, when using C language to design a single-chip microcomputer application system program, the first thing to pay attention to is to use a structured programming method as much as possible, so that the entire application system program structure can be clear and easy to debug and maintain. For a larger application, the entire program is usually divided into several modules according to function, and different modules perform different functions. Each module can be written separately, or even by different programmers. Generally, the functions performed by a single module are relatively simple, and the design and debugging are relatively easy. In C language, a function can be considered as a module. The so-called program modularization is not only to divide the entire program into several functional modules, but more importantly, it should also pay attention to maintaining the relative independence of variables between modules, that is, to maintain the independence of modules, and try to use less global variables. For some commonly used functional modules, they can also be encapsulated as an application library so that they can be directly called when needed. However, when using modularization, if the modules are divided too finely and too small, the execution efficiency of the program will be low (protecting and restoring registers when entering and exiting a function takes some time).
4. Define constants
In the process of programming design, if you write some frequently used constants directly into the program, once the value of the constant changes, you must find all the constants in the program one by one and modify them one by one, which will inevitably reduce the maintainability of the program. Therefore, you should try to use the preprocessing command method to define constants, which can also avoid input errors.
5. Reduce judgment statements.
Use conditional compilation (ifdef) instead of if statements where possible, which helps reduce the length of the compiled code.
6.
When the priority of various operations in an expression is unclear or confusing, parentheses should be used to specify their priority. An expression should not be too complicated. If it is too complicated, it will be difficult to understand after a long time, which is not conducive to future maintenance.
7. Functions
For functions in a program, the type of the function should be described before use. The description of the function type must ensure that it is consistent with the originally defined function type. For functions without parameters and return value types, the "void" description should be added. If you need to shorten the length of the code, you can define some common program segments in the program as functions. This is the high-level optimization in Keil. If you need to shorten the execution time of the program, replace some functions with macro definitions after the program is debugged. Note that the macro should be defined after the program is debugged, because most compilation systems will report errors only after the macro is expanded, which will increase the difficulty of debugging.
8. Use global variables as little as possible and local variables as much as possible. Because global variables are placed in the data memory, defining a global variable means that the MCU has one less available data memory space. If too many global variables are defined, the compiler will not have enough memory to allocate. Local variables are mostly located in registers inside the MCU. In most MCUs, register operations are faster than data memory, and instructions are more numerous and flexible, which is conducive to generating higher quality code. In addition, the registers and data memory occupied by local variables can be reused in different modules.
9. Set appropriate compiler options
Many compilers have several different optimization options. Before using them, you should understand the meaning of each optimization option and then choose the most appropriate one. Usually, once the highest level of optimization is selected, the compiler will pursue code optimization almost pathologically, which may affect the correctness of the program and cause program errors. Therefore, you should be familiar with the compiler you are using and know which parameters will be affected during optimization and which parameters will not be affected.
In ICCAVR, there are two optimization options: "Default" and "Enable Code Compression". In
CodeVisionAVR, there are two memory modes: "Tiny" and "small".
In IAR, there are 7 different memory mode options.
In GCCAVR, there are more optimization options, and it is easier to choose inappropriate options if you are not careful.
2. Code Optimization
1. 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 introduce them. Replacing the slower sequential search method with a faster binary search or random search method, and replacing the insertion sort or bubble sort 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. For example, if you use a lot of insertion and deletion instructions in a bunch of randomly stored numbers, it will be much faster to use a linked list.
Arrays and pointers 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 and more efficient than that of arrays. But in Keil, on the contrary, the code generated by using arrays is shorter than that of using pointers.
2. Use the smallest data type
possible. If you can define a variable with a character type (char), do not define it with an integer type (int); if you can define a variable with an integer type, do not use a long int; if you can avoid using a floating point type (float), do not use a floating point type (float). Of course, do not exceed the scope of the variable after defining it. If you assign a value that exceeds the scope of the variable, the C compiler will not report an error, but the program will run incorrectly, and such errors are difficult to find.
In ICCAVR, you can set the use of printf parameters in Options, 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 specifiers), and try not to use floating point parameters (%f). The same applies to other C compilers. If other conditions remain unchanged, using the %f parameter will increase the amount of generated code and reduce the execution speed.
3. Use self-increment and self-decrement instructions.
Usually, the use of self-increment and self-decrement instructions and compound assignment expressions (such as a-=1 and a+=1, etc.) can generate high-quality program codes. Compilers can usually generate instructions such as inc and dec. When using instructions such as a=a+1 or a=a-1, many C compilers will generate two to three bytes of instructions. The codes generated by the above writing methods are the same in ICCAVR, GCCAVR, IAR and other C compilers applicable to AVR chips, and can also generate high-quality inc and dec codes.
4. Reduce the intensity of operations.
You can use expressions with smaller operations but the same functions to replace the original complex expressions. As follows:
(1) Remainder operation.
a=a%8;
can be changed to:
a=a&7;
Note: Bit operations only need one instruction cycle to complete, while most C compilers use subroutines to complete the "%" operation, which has long code and slow execution speed. Usually, if you only need to find the remainder of the 2n square, you can use the bit operation method to replace it.
(2) Square operation
a=pow(a,2.0);
can be changed to:
a=a*a;
Note: In microcontrollers with built-in hardware multipliers (such as the 51 series), multiplication is much faster than square operation, because the square of floating point numbers is implemented by calling subroutines. In AVR microcontrollers with built-in hardware multipliers, such as ATMega163, multiplication can be completed in only 2 clock cycles. Even in AVR microcontrollers without built-in hardware multipliers, the subroutine for multiplication is shorter than the subroutine for square operation and executes faster.
If the cube is calculated, such as:
a=pow(a,3.0);
is changed to:
a=a*a*a;,
the efficiency improvement is more obvious.
(3) Use shifting to implement multiplication and division operations
a=a*4;
b=b/4;
can be changed to:
a=a<<2;
b=b>>2;
Note: Usually, if you need to multiply or divide by 2n, you can use the shifting method instead. In ICCAVR, if you multiply by 2n, you can generate left shift code, and multiply by other integers or divide by any number, call the multiplication and division subroutine. The code generated by the shifting method is more efficient than the code generated by calling the multiplication and division subroutine. In fact, as long as you multiply or divide by an integer, you can use the shifting method to get the result, such as:
a=a*9
can be changed to:
a=(a<<3)+a
5. Loops
(1) Loop statements
For tasks that do not require loop variables to be used in calculations, they can be placed outside the loop. Tasks here include expressions, function calls, pointer operations, array accesses, etc. Operations that do not need to be performed multiple times should be grouped together and placed in an initialization procedure called init.
(2) Delay function:
The commonly used delay function is in the form of self-increment:
void delay (void)
{
unsigned int i;
for (i=0;i<1000;i++)
;
}
Change it to a self-decrement delay function:
void delay (void)
{
unsigned int i;
for (i=1000;--i;)
;
}
The delay effects of the two functions are similar, but almost all C compilers generate 1~3 bytes less code for the latter function than for the former, because almost all MCUs have instructions for transferring to 0, and the latter method can generate such instructions.
The same is true when using a while loop. Using a self-decrement instruction to control the loop will generate 1~3 fewer letters of code than using a self-increment instruction to control the loop.
However, when there are instructions in the loop that read and write arrays through the loop variable "i", using a pre-decrement loop may cause the array to exceed the bounds, so be careful.
(3) while loop and do...while loop
When using the while loop, there are the following two loop forms:
unsigned int i;
i=0;
while (i<1000)
{
i++;
//user program
}
or:
unsigned int i;
i=1000;
do
i--;
//user program
while (i>0);
Of these two loops, the length of the code generated after compiling using the do...while loop is shorter than that of the while loop.
6. Table lookup
is generally not used in programs for very complex operations, such as multiplication, division and square root of floating-point numbers, as well as interpolation operations of some complex mathematical models. For these operations that consume both time and resources, table lookup should be used as much as possible, and the data table should be placed in the program storage area. If it is difficult to directly generate the required table, try to calculate it at startup, then generate the required table in the data storage, and then directly look up the table when the program is running, reducing the workload of repeated calculations during program execution.
7. Other methods
, such as using online assembly and storing strings and some constants in program memory, are beneficial to optimization.
Previous article:Write a printf() function that suits your needs
Next article:MCU power supply and decoupling
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Supercapacitor constant power charging and voltage regulation
- EEWORLD University ---- Big Data Machine Learning (Yuan Chun)
- Boss, what's your home WIFI password? ——Qorvo~Wi-Fi 6
- When BLE meets MEMS——attitude calculation
- Lazy voice-controlled lamp based on Gizwits Cloud
- 【NXP Rapid IoT Review】+ Review Summary
- Has anyone used Bosch's PM sensor?
- [NXP Rapid IoT Review] +1. Unboxing and Getting to Know the Rapid IoT Kit
- HFSS simulation software field strength pattern and 3D diagram
- Embedded software squeezes out the lowest power mode