Using the features of the programming language can not only simplify the program, but also improve the execution efficiency of the program. First, let's look at an example program that uses sizeof () to improve program efficiency. Figure 1 shows the code before sizeof () is not used. The background information must be explained here. Here, the alarm_string variable is defined as a char array with a length of 255, while tail_msg defines a pointer to the string ", List NOT Complete. Space is used to obtain the amount of space that can be used to store other content after tail_msg points to the length of the string in alarm_string. This is because in some cases, copy the string pointed to by tail_msg to the end of the alarm_string array. In addition, the content of the string pointed to by tail_msg will not be changed.
Example. c
00070: # define MAX_STRING_TXT 255
00071: char alarm_string [MAX_STRING_TXT];
00072:
00073: char * tail_msg = ", List NOT Complete ";
00074: char space = MAX_STRING_TXT-strlen (tail_msg)-1;
Figure 1
In Figure 1, the strlen () function is used to obtain the length of the string pointed to by tail_msg to calculate the space value () the string Terminator '\ 0' is not included, so the last side of space must be reduced by one. As strlen () is a function, strlen () is also called every time this short code in Figure 1 is executed, which obviously takes a certain amount of processing time, better solution 2 is shown.
Example. c
00070: # define MAX_STRING_TXT 255
00071: char alarm_string [MAX_STRING_TXT];
00072:
00073: static const char tail_msg [] = ", List NOT Complete ";
00074: char space = MAX_STRING_TXT-sizeof (tail_msg );
Figure 2
Figure 2 defines tail_msg as a static array and uses sizeof () in space Variable calculation. Note that sizeof () the string Terminator '\ 0' is included. Because the value of sizeof () is determined at compilation, that is, for the example here, the compiler calculates that the value of sizeof (tail_msg) should be 20 at compilation, therefore, space will be directly bin-valued as 235 at runtime, without any function calls or mathematical operations. In addition, you also need to note that tail_msg must be defined as static and const. Otherwise, the compiler will generate a piece of code. Every time this program is executed, the tail_msg array located on the stack will be initialized. Defining tail_msg as static and const will cause its memory to be allocated on the. rodata segment, see section in the program) instead of the stack, so as to avoid multiple initialization operations.
Next we will look at another example using programming language features, as shown in the original example 3. Call memset () in Row 3 to set the partial array variable temp to zero initialization. Obviously, due to the existence of the memset () function, this code must call the memset () function every time it runs, better solution 4 is shown.
Example. c
00141: # define MAX_MSK_OCTET_LEN 64
00142: char temp [MAX_MSK_OCTET_LEN];
00143: memset (temp, 0, sizeof (temp ));
Figure 3
In Figure 4, a bind value with zero Initialization is added at the end of the temp variable. When the compiler sees this code, code is generated to set zero initialization for all memory (64 bytes) pointed to by temp. In this way, you can call the memset () function to Improve the efficiency.
Example. c
00141: # define MAX_MSK_OCTET_LEN 64
00142: char temp [MAX_MSK_OCTET_LEN] = {0 };
Figure 4
Obviously, to make good use of the features of the programming language, you need to have a deeper understanding of the programming language, not just the knowledge introduced in some introductory books. Although the efficiency brought by the use of programming language features is negligible for the powerful processor, it is more representative of our professionalism-skillful control of programming language!
This article from "to Jane Li cloud" blog, please be sure to keep this source http://yunli.blog.51cto.com/831344/252954