A few tips for efficient C language programming-general Linux technology-Linux programming and kernel information. The following is a detailed description. Reference:
Writing efficient and concise C language code is the goal pursued by many software engineers. This article will describe some experiences and experiences in your work. If not, please advise.
1st tips: Change the time by Space
The biggest contradiction in computer programs is the contradiction between space and time. From this perspective, we should reverse thinking to consider the program efficiency, we now have 1st moves to solve the problem-change the space for time.
For example, assign a value to a string.
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;
(You can directly use a pointer to perform operations .)
From the above example, we can see that the efficiency of A and B is incomparable. In the same bucket, B can directly use the pointer, and A needs to call two character functions to complete the operation. B's disadvantage is that flexibility is not as good as. When you need to change the content of A string frequently, A has better flexibility. If you use method B, you need to pre-store many strings, although it occupies A large amount of memory, however, the program execution efficiency is achieved.
If the system has high real-time requirements and memory requirements, I recommend that you use this method.
This variable is a macro function instead of a function. Example:
Method C:
# Define bwmcr2_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 ))))
}
The difference between a function and a macro function is that a macro function occupies a large amount of space while a function occupies time. You need to know that function calling uses the system stack to store data. If the compiler has the stack check option, generally, some Assembly statements are embedded in the function header to check the current stack. At the same time, the CPU also needs to save and restore the current site during function calling to perform stack pressure and elastic stack operations, therefore, it takes some CPU time to call a function. This problem does not exist in macro functions. 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 ARM's source code and implements many functions in just three lines, covering almost all bit operation functions. The C method is its variant, and the taste needs to be carefully understood.
2nd tips: mathematical methods to solve problems
Now we assume 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.
For example, 1 ~ Sum of 100.
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 mathematical case that I was most impressed with. It was tested by my computer teacher. At that time, I only had a third-grade primary school. Unfortunately, I didn't know how to use the formula N × (N + 1)/2 to solve this problem. Method E loops 100 times to solve the problem. That is to say, at least 100 assignments, 100 judgments, and 200 additions (I and j) are used ); method F only uses one addition, one multiplication, and one division. The effect is self-evident. So now, when I compile a program, I use my brains to find patterns and maximize the power of mathematics to improve the 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. Example:
Method G
Int I, J;
I = 257/8;
J = 456% 32;
Method H
Int I, J;
I = 257> 3;
J = 456-(456> 4 <4 );
Literally, H is much more troublesome than G. However, you can check the generated assembly code carefully to understand that the method gcall uses the basic modulo function and division function, which can be called by both functions, there are also a lot of assembly code and registers involved in the operation, while method H is just a few related assembly, the code is more concise, more efficient. Of course, due to the differences in compilers, there may be little difference in efficiency. However, from the perspective of ms c and arm c I have encountered, the efficiency gap is not small. The relevant Assembly Code is not listed here.
When using this method, you must note that the CPU usage is different. For example, a program written on a PC that passes debugging on a PC may cause code risks when it is transplanted to a 16-bit platform. Therefore, this approach can be used only on the basis of certain advanced technologies.
4th tips: Assembly embedding
The fourth measure is embedded assembly.
"In the eyes of people familiar with assembly languages, C-language programs are junk ". Although this argument is somewhat radical, it makes sense. Assembly language is the most efficient computer language, but it cannot be used to write an operating system? Therefore, to achieve program efficiency, we had to adopt a flexible method-Embedded Assembly, mixed programming.
For example, assign array 1 to array 2, and each byte must be consistent.
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
Method I is the most common method and uses 1024 cycles. Method J is differentiated based on different platforms. On the ARM platform, the same operation is completed with only 128 cycles of Embedded Assembly. Some may say, why not use a standard memory copy function? This is because the source data may contain 0 bytes of data. In this case, the standard library function will end early without completing the required operations. This routine is typically used to copy LCD data. Using Embedded Assembly based on different CPUs can greatly improve program execution efficiency.
Although it is mandatory, it will be costly to use it easily. This is because the use of Embedded Assembly restricts the portability of the program, so that the program can be transplanted on different platforms! At the same time, this trick is contrary to the modern software engineering idea, and can be used only when it is forced. Remember, remember.
I use C language for efficient programming. Here, I would like to introduce you to the discussion. I hope you can give us a better way to improve our programming skills.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.