1. Pointer:
For most compilers, the code generated by using pointers is shorter and more efficient than that generated by using arrays. However, in Keil, the opposite is true. The code generated by using arrays is shorter than that generated by using pointers. 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 code. The compiler 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.
2. Remainder operation:
a=a%8; --> a=a&7;
Note: Bit operations can be completed in just one instruction cycle, while most C compilers use subroutines to complete the "%" operation, which results in long code and slow execution. Usually, if you only need to find the remainder of a square of 2n, you can use bit operations instead.
3. Square operation:
a=pow(a,2.0); --> a=a*a;
Note: In microcontrollers with built-in hardware multipliers (such as the 51 series), multiplication is much faster than squaring because the squaring of floating-point numbers is achieved by calling a subroutine. In AVR microcontrollers with built-in hardware multipliers, such as ATMega163, multiplication can be completed in just 2 clock cycles. Even in AVR microcontrollers without built-in hardware multipliers, the subroutine for multiplication is shorter in code and faster in execution than the subroutine for squaring.
If we are looking for the cube, such as: a=pow(a,3.0); --> a=a*a*a; the improvement in efficiency is more obvious.
4. Multiplication and division:
a=a*4; --> a=a<<2;
b=b/4; --> b=b>>2;
In fact, as long as you multiply or divide by an integer, you can use the shift method to get the result, such as:
a=a*9 --> a=(a<<3)+a
5. Delay function:
The commonly used delay functions are in the form of self-increment:
void delay (void)
{
unsigned int i;
for (i=0;i<1000;i++)
;
}
Change it to a self-decrementing delay function
void delay (void)
{
unsigned int i;
for (i=0;i<1000;i--)
;
}
The delay effects of the two functions are similar, but the code generated by almost all C compilers for the latter function is 1 to 3 bytes less than the former code, because almost all MCUs have instructions for transferring to 0, and the latter method can generate such instructions.
6. While loop and do...while loop
Of the two loops, the length of the code generated by the do...while loop after compilation is shorter than that of the while loop. To write good C language, beautiful macro definitions are very important. Using macro definitions can prevent errors, improve portability, readability, convenience, etc.
Previous article:Several issues to note about C51 interrupt functions
Next article:C51 library function accumulation
Recommended ReadingLatest update time:2024-11-16 12:58
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
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