1. Time and Space Problems
In the field of embedded systems, we often encounter time and space problems. If some systems have high real-time requirements, we need to use space for time,
Method:
# Define Len 32
Char string1 [Len];
Memset (string1, 0, Len );
Strcpy (string1, "This is a example !!");
Method B:
Const char string2 [Len] = "This is a example !";
Char * CP;
CP = string2;
Although this method is clumsy and lacks flexibility, the efficiency is relatively high under special circumstances.
2. Macro Problems
Generally, macros are not recommended because they are damaged.ProgramReadability, especiallyCodeWhen you dive into a lot of macros, troubleshooting is very troublesome! However, in special circumstances, for example, when efficiency is pursued, we can still compile the work, because modern compilers automatically extend small or inline functions to macros. (I usually only use macros to define constants and compile conditions ).
3. Optimize the program using mathematical methods
Sometimes this problem is often ignored, especially for unexperienced programmers. However, the difference in efficiency is huge. A classic example is:
1 ~ Sum of 100.
Method:
Int I, J;
For (I = 1; I <= 100; I ++)
{
J + = I;
}
Method B:
Int I;
I = (100*(1 + 100)/2
Obviously, the difference here is huge, and the code of Method B is also more concise. Haha ^ _ ^, so the programmer's poor mathematics is also a factor limiting development!
4. Use bitwise operations instead of multiplication and division
We all know that multiplication and division cannot be carried out directly in the CPU. The CPU uses addition to present the results. Therefore, we need to use displacement to realize multiplication and division under appropriate circumstances. For example:
A = 211/4; can also be written as a = 211> 2; (when appropriate here, of course, when the formula is not very complex, if you need to use displacement for operations, so it will be painful to call the program ^_^ )!
5. Integrated assembly
Everyone knows that assembly is the fastest program language in the computer, and the program efficiency can be improved by an order of magnitude when assembly is used, but the so-called two sides of things, the use of Assembly also reduces the portability of the program, and may cause huge damage to the program. it is recommended that you use it with high level of knowledge, and only when the underlying processing or performance requirements are very demanding. for example:
Char string1 [1, 1024], string2 [1024];
Method I
Int I;
For (I = 0; I <1024; I ++)
* (String2 + I) = * (string1 + I)
Method J
# Ifdef _ pc _
Int I;
For (I = 0; I <1024; I ++)
* (String2 + I) = * (string1 + I );
# Else
# Ifdef _ arm _
_ ASM
{
MoV r0, string1
MoV R1, string2
MoV R2, #0
Loop:
Ldmia R0 !, R3-R11
Stmia R1 !, R3-R11
Add R2, R2, #8
CMP R2, #400
BNE Loop
}
# Endif
Note: The Example code here carries the website www.vckbase.com!