Quote: Writing an efficient and concise 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. 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: #define len 32char string1 [len]; MEMSET (String1, 0, len); struct (string1, "this is a example !!); method B: const char string2 [len] =" This is a example! "; Char * cp; cp = string2 (can be used directly with a pointer to manipulate.) 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: 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 (__ bf) (((1U << (bw ## __bf)) - 1) < <(BS ## __BF) #DINE 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 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 EINT I, J; For (i = 1 i <= 100; i ) {j = i;} method FINT I; I = (100 * (1 100)) / 2 This example is the deepest A mathematical case, 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 Gint i, j; i = 257/8; j = 456% 32; method Hint i, j; i = 257 >> 3; j = 456 - (456 >> 4 << 4); It seems that H is more troublesome, but 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; Method h is just a few related assembly, the code is more concise, 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.