C language efficient programming 2003-10-08 [中] [] China University of Science and Technology Diography [Wen] Statement: This website is from the original, netizens of this site, and picking from elsewhere, etc. Downloading and use, we must fully respect the copyright of the work, and strictly enforce the resource from different channels. This site refuses everything with commercial colors and downloads. Wan Wang pay attention, this statement! 1 x Quotational: Write an efficient and concise C language code, which is the goal of many software engineers pursued. This article explains some of the experiences and experiences in the work, please advise. The first move: With the biggest contradiction in the computer program in the spatial change, it is the contradiction between space and time, then from this perspective to consider the efficiency problem of the program, we have the first trick to solve the problem - with space Transfer time. For example: the assignment of the string. Method A, usual approach:
PHP source code:
#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;
(Use the pointer to be used directly.) It can be seen from the above example, and the efficiency of A and B cannot be compared. Under the same storage space, B can be operated directly using the pointer, and A needs to call two character functions to complete. B disadvantage is that the flexibility is not A good. When you need frequent changes in a string content, A has better flexibility; if you use method B, you need to pre-store a number of strings, although a lot of memory is occupied, but the efficiency of the program execution is obtained. If the system's real-time requirements are high, there are some memory, then I recommend you to use this trick. This trick change - uses the macro function instead of a function. Examples are as follows: Method C:
PHP source code:
#define bWMCDR2_ADDRESS 4
#define BSMCDR2_ADDRESS 17
INT BIT_MASK (int __bf)
{
Return ((1u << (BW ## __BF)) - 1) << (BS ## __bf);
}
Void set_bits (int __dst, int __bf, int __val)
{
__dst = ((__dst) & ~ (Bit_Mask (__ bf))) |
((__val) << (BS ## __BF)) & (Bit_Mask (__ bf))))))))
}
Set_bits (MCDR2, MCDR2_ADDRESS, RegisterNumber);
Method D:
#define bWMCDR2_ADDRESS 4
#define BSMCDR2_ADDRESS 17
#DEFINE BMMCDR2_ADDRESS BIT_MASK (MCDR2_ADDRESS)
#define bit_mask (__ bf) ((1U << (BW ## __BF)) - 1) << (BS ## __bf))
#define set_bits (__ dst, __bf, __val)
((__dst) = ((__dst) & ~ (Bit_Mask (__ bf))) |
((__val) << (BS ## __BF)) & (Bit_Mask (__ bf))))))))
Set_bits (MCDR2, MCDR2_ADDRESS, RegisterNumber);
The difference between functions and macro functions is that the macro function takes up a lot of space, while the function takes up time. Everyone is to know, the function call is to save the data using the system's stack. If there is a stack check option in the compiler, it is generally inspected in the function of the function; at the same time, the CPU is also The function call saves and restores the current field, performs the stack and playback operation, so the function call requires some CPU time. And the macro function does not exist. The macro function is only embedded in the current program as a pre-written code, and does not generate a function call, so it is only occupied space, which is especially prominent when the same macro function is frequently called. The D method is the best set operation function I have seen, which is part of the ARM source code, which implements a lot of features in a short three lines, which covers all bit operation functions. The C method is its variant, which is also required to care carefully. The second trick: Mathematical method solves the problem. Now we interpret the second trick written in high-efficiency C language - using mathematical methods to solve problems. Mathematics is the mother of the computer, no mathematical basis, and there is no computer development, so when writing the program, some mathematical methods have an increase in the implementation efficiency of the procedure. For example, find 1 to 100 and. Method EPHP source code:
INT I, J;
For (i = 1; i <= 100; i ) {
J = I;
}
Method F
INT I;
I = (100 * (1 100)) / 2
This example is a mathematics for my impression, is my computer enlightenment teacher taking me. At that time, I only had the third grade of elementary school, but I didn't know if I used formula N × (n 1) / 2 to solve this problem. Method E Circulate 100 times to solve the problem, that is, the least 100 assignments, 100 judgments, 200 addition (i and j); method F only uses 1 addition, 1 multiplication, 1 division . The effect is naturally self-evident. So, now I am in the program, more is the rules of brains, and maximize the power of mathematics to improve the efficiency of the program operation. Third stroke: Use the bit operation to achieve the third stroke prepared by high-efficiency C language - use the bit operation, reduce the division of division and the molding. In a computer program, the data of the data is the minimum data unit that can be manipulated, theoretically use "bit operation" to complete all the operations and operations. The general bit operation is used to control hardware, or do data transformation, but the flexible bit operation can effectively improve the efficiency of the program operation. For example: Method G
PHP source code:
INT I, J;
I = 257/8;
J = 456% 32;
Method h
INT I, J;
I = 257 >> 3;
J = 456 - (456 >> 4 << 4);
It seems that H is more troublesome on the literal, however, the assembly code that carefully views will understand that Method G calls the basic model function and division function, existing function calls, and many assembly code and register participation operations The method h is just a few related assembly, the code is more concise, and the efficiency is higher. Of course, due to the difference in the compiler, the difference in efficiency is not large, but in the MS C, ARM C I have now, the gap between efficiency is still small. The relevant assembly code is not listed here. Using this trick To note that problems arising from the different CPUs. For example, using this program written in the PC and debug on the PC, when porting to a 16-bit machine platform, code hidden dangers may occur. Therefore, this trick can only be used at a certain technical advancement. The 4th Top: Compilation Embedded Efficient C Language Programming Kill Technology, Fourth Touch - Embed Compilation. "In the human eye that is familiar with the assembly language, the procedures written in C are garbage. This kind of saying is evenly, but it has its truth. The assembly language is the highest efficiency computer language, but it is impossible to write an operating system by it? So, in order to obtain the efficiency of the program, we have to use a variable method - embedding assembly, mix programming. For example, an array is assigned to an array two, requiring each byte to match. PHP source code:
CHAR STRING1 [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
Method I is the most common method, using 1024 cycles; method j makes differentiated according to the platform, under the ARM platform, using embedded compilation only with 128 cycles to complete the same operation. Here is a friend who will say, why don't you use a standard memory copy function? This is because in the source data may contain 0 bytes of data 0, the standard library function will end in advance without completing our requirements. This routine is typically applied to the copy process of the LCD data. According to different CPUs, skilled use of corresponding embedding assembly, it can greatly improve the efficiency of the program. Although it is a must kill, it will pay a heavy cost if it is easy to use. This is because used embedded compilation, limits the portability of the program, so that the procedure is in the process of transplantation of different platforms, and the tiger is the dragon, and the danger is coming. At the same time, the trick is also contrary to the idea of modern software engineering, and can only be adopted if it is forced. Remember, remember. Use the C language to conduct high efficiency programming, my experience is only. Here, this article will be thrown into jade, please also ask all the masters to learn together. I hope that you can give a better way, everyone improves our programming skills.