Writing efficient and concise C language code is the goal pursued by many software engineers. Get up!
First move: Change Time with Space
The biggest contradiction in computer programs is the contradiction between space and time. From this point of view, reverse thinking is used to consider the program efficiency.
Eg. String assignment
LEN 32 string2[LEN]= *=string2;
From the example above, we can see that the efficiency of A and B is incomparable. In The Same bucket, B can directly use pointers to perform operations, A needs to call two character functions to complete the process. B's disadvantage is that flexibility is not good. A is more flexible when it needs to frequently change the content of A string. If Method B is used, many strings need to be pre-stored. Although a large amount of memory is occupied, the program execution efficiency is achieved.
If the system has high real-time requirements and memory requirements, I recommend this feature.
The Macro function rather than the function is used for the edge action of this tactic. The sdigit for completing the I/O getchar () and character testing is an example recognized by the conventional approach, the most fundamental reason for doing so is execution efficiency. Macros can avoid the overhead of function calls.
bwMCDR2_ADDRESS 4 bsMCDR2_ADDRESS 17 BIT_MASK( ((IU<<(bw## _bf))-) << SET_BITS( _dst, _bf,=((_dst)&~(BIT_MASK(_bf)))|<<(bs##_bf)& bwMCDR2_ADDRESS 4 bsMCDR2_ADDRESS 17 bmMCDR2_ADDRESS BIT_MASK(MCDR2_ADDRESS) BIT_MASK(_bf)(((IU<<(bw## _bf))-1) << (bs##_bf)) SET_BITS(_dst,_bf,_val)\&~(BIT_MASK(_bf)))| (((_val)<<(bs##_bf)&
The difference between a macro function and a function is that a macro function occupies a large amount of space and the function takes up time. You must know that function calling uses the system stack to store data, if there is a stack check option in the compiler, some Assembly statements will be sneak into the function header to check the current stack. At the same time, the CPU also needs to save and restore the current scene during function calling, therefore, function calls require some CPU events, while macro functions do not. Macro functions are only embedded into the current program as pre-written code and do not generate function calls. Therefore, they only occupy space. This phenomenon is particularly prominent when the same macro function is frequently called.
The D method is the best position operation function I have seen. It is part of the ARM's source code and has implemented many functions in just three lines, covering almost all the bit operation functions, the C method is its variant, and you need to know it carefully !!
2nd tips: mathematical methods to solve problems
Now we are playing the second trick of writing efficient C language-using mathematical methods to solve the problem.
Mathematics is the mother of computers. Without the foundation and foundation of mathematics, there will be no computer development. Therefore, when programming, using some mathematical methods will increase the execution efficiency of the program by an order of magnitude.
1 ~ Sum of 100
i,j=(i=;i<;i++=j+I=(*(+))/
Obviously, the effect is self-evident, so when we compile a program, we need to use our brains to find patterns and maximize the power of mathematics to improve program running efficiency.
3rd tips: Bit operations
The third way to achieve efficient C language writing is to use bitwise operations to reduce division and modulo operations.
In computer programs, the bit of data is the smallest unit of data that can be operated. In theory, you can use the bit operation to complete all the operations and operations. Generally, bit operations are used to control hardware or perform data transformation. However, flexible bit operations can effectively improve the efficiency of program running.
=/=%=>>=-(>>>>);
Literally, H is much more troublesome than G. However, if you carefully check the generated assembly code, you will understand that the method gcall uses the basic modulo and Division functions, both function calls, there are also a lot of assembler programs and registers involved in the operation, and method H is only a very relevant Assembly, the code is more concise, more efficient. Of course, due to different compilers, the efficiency gap may be small.
Use this trickNote thatFor example, the program written on the Pc and debugged on the pC until it is on a 16-bit sub-platform, this vulnerability may cause code risks. Therefore, this vulnerability can be used only on the basis of advanced technologies.
I can only use the C language for efficient programming. I hope you can give us a better way to improve our programming skills.