introduction:
Write an efficient and simple C language code is the goal pursued by many software engineers. This article explains some of the experiences and experiences in the work, please advise.
First trick: Take time
The biggest contradiction in the computer program is the contradiction between space and time. So from this angle to think about the efficiency of the program, we have the first trick to solve the problem - with a space change time.
For example: the assignment of the string.
Method A: Usually the usual approach:
#define len 32char 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;
You can use the pointer to operate directly.
As can be seen from the example above, the efficiency of A and B cannot be more than. 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. For example, as follows:
Method C:
#define bwmcdr2_address 4 # define bsmcdr2_address 17INT 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 ((1U << (BS ## __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.
Second: Mathematical methods solve problems
Now we interpret the second trick written in efficient 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 E
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 bit operation
Third strokes prepared by achieving efficient C language - use bit operation. Reduce the operation of division and 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, as follows:
Method G
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 fourth stroke: compilation embedding
High-efficiency C language programming must kill technology, fourth stroke - embedding assembly. "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.
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, # 0loop : 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.