Introduction to the use of C language in the programming process

<div class="iteye-blog-content-contain" style="font-size: 14px"><p><div id="Article"></p>
<p>  Introduction: < /p>
<p> <p>Writing efficient and concise C language code is the goal pursued by many software engineers. This article makes some relevant expositions on some experiences and experiences in the work, and please advise me on the wrong places. </p> </p>
<p> <p>Trick 1: Exchange space for time</p> </p>
<p> <p>Computer program The biggest contradiction in the program is the contradiction between space and time. Then, from this perspective, thinking backwards to consider the efficiency of the program, we have the first trick to solve the problem—exchange space for time. </p> </p>
<p> <p>Example: String assignment. </p> </p>
<p> <p>Method A, the usual method: </p> </p>
<p>
















<p> <p>If <a href="https://www.2cto.com/os/" target="_blank" class="keylink">system</a>real-time The requirements are very high, and there is still some memory, then I recommend you to use this trick. </p> </p>
<p> <p>Variation of this trick——Use a macro function instead of a function. For example: </p> </p>
<p> <p>Method C: </p> </p>
<p> <pre class="brush: <a href="http://www.cppentry.com/list.php?fid=76">JAVA</a>;"></p>
<p>#define bwMCDR2_ADDRESS 4</p>
<p> </p>
<p>#define bsMCDR2_ADDRESS 17</p>
<p> </p>






<p>}</p>
<p> </p>
<p>void SET_BITS(int __dst, int __bf, int __val)</p>
<p> </p>
<p>{</p>
<p> </p>
<p>__dst = ((__dst) &amp; ~(BIT_MASK(__bf))) | /</p>
<p> </p>
<p>(((__val) &lt;&lt; (bs ## __bf)) &amp; (BIT_MASK(__bf))))</p>
<p> </p>
<p>}</p>
<p> </p>
<p>SET_BITS(MCDR2, MCDR2_ADDRESS, RegisterNumber);</pre> </p>
<p> <p>方法D:</p> </p>
<p> <pre class="brush:<a href="http://www.cppentry.com/list.php?fid=76">JAVA</a>;"></p>
<p>#define bwMCDR2_ADDRESS 4</p>
<p> </p>
<p>#define bsMCDR2_ADDRESS 17</p>
<p> </p>
<p>#define bmMCDR2_ADDRESS BIT_MASK(MCDR2_ADDRESS)</p>
<p> </p>
<p>#define BIT_MASK(__bf) (((1U &lt;&lt; (bw ## __bf)) - 1) &lt;&lt; (bs ## __bf))</p>
<p> </p>
<p>#define SET_BITS(__dst, __bf, __val) /</p>
<p> </p>
<p>((__dst) = ((__dst) &amp; ~(BIT_MASK(__bf))) | /</p>
<p> </p>
<p>(((__val) &lt;&lt; (bs ## __bf)) &amp; (BIT_MASK(__bf))))</p>
<p> </p>
<p>SET_BITS(MCDR2, MCDR2_ADDRESS, RegisterNumber);</pre> </p>
<p> <p>The difference between functions and macro functions is that macro functions take up a lot of space, while functions take up time. What everyone needs to know is that the function call uses the system stack to save data. If the compiler has a stack check option, usually some assembly statements will be embedded in the function header to check the current stack; at the same time, the CPU must also be in When the function is called, the current scene is saved and restored, and the stack is pushed and popped. Therefore, the function call requires some CPU time. The macro function does not have this problem. The macro function is only embedded into the current program as a pre-written code, and does not generate a function call, so it only takes up space. This phenomenon is especially prominent when the same macro function is frequently called. </p> </p>
<p> <p> The D method is the best set operation function I have seen, and is the ARM company<a href="https://www. 2cto.com/ym/" target="_blank" class="keylink">Part of the source code</a>, it implements a lot of functions in just three lines, covering almost all bit manipulation functions. The C method is a variant of it, and the taste of it needs to be carefully understood by everyone. </p> </p>
<p> <p>Trick 2: Mathematical Problem Solving</p> </p>
<p> <p>Now we The second trick to deductive efficient C programming—using mathematics to solve problems. </p> </p>
<p> <p>Mathematics is the mother of computers. Without the basis and foundation of mathematics, there will be no development of computers. Therefore, when writing programs, using some mathematical methods will improve the execution efficiency of programs by orders of magnitude. </p> </p>
<p> <p>For example, find the sum of 1~100. </p> </p>
<p> <p>方法E</p> </p>
<p> <pre class="brush:<a href="http://www.cppentry.com/list.php?fid=76">JAVA</a>;"></p>
<p>int I , j;</p>
<p> </p>
<p>for (I = 1 ;I&lt;=100; I ++){</p>
<p> </p>
<p>j += I;</p>
<p> </p>
<p>}</pre> </p>
<p> <p>方法F</p> </p>
<p> <pre class="brush:<a href="http://www.cppentry.com/list.php?fid=76">JAVA</a>;"></p>
<p>int I;</p>
<p> </p>
<p>I = (100 * (1+100)) / 2</pre> </p>
<p> <p>This example is one of the math use cases that impressed me the most. It was tested by my computer enlightenment teacher. I was only in the third grade of elementary school at the time, but unfortunately I didn't know how to solve this problem with the formula N&times;(N+1)/2. Method E needs to loop 100 times to solve the problem, which means that at least 100 assignments, 100 judgments, and 200 additions (I and j) are used; while method F only uses 1 addition, 1 multiplication, and 1 division . The effect is self-evident. So, now I'm in <a href="https://www.2cto.com/kf" target="_blank" class="keylink">programming</a> Find the law with your brain, maximize the power of mathematics to improve the efficiency of program operation. </p> </p>
<p> <p>Trick 3: Use bit manipulation</p> </p>
<p> <p> The third trick written in C language - using bit operations, reduces division and modulo operations. </p> </p>
<p> <p> In a computer program, a bit of data is the smallest unit of data that can be manipulated. In theory, "bit operations" can be used to complete all operations and operations. The general bit operation is used to control hardware, or to do data transformation, but flexible bit operation can effectively improve the efficiency of program operation. For example: </p>&


<p>int I,J;</p>
<p> </p>
<p>I = 257 /8;</p>
<p> </p>
<p>J = 456 % 32;</p>
<p> </p>
<p>Method H</p>
<p> </p>
<p>int I,J;</p>
<p>  </p>
<p>I = 257 &gt;&gt;3;</p>
<p> </p>
<p>J = 456 - (456 &gt;&gt; 4 &lt;&lt; 4);</pre> </p>
<p> <p> Literally it seems that H is much more troublesome than G, however, look closely at the resulting assembly The code will understand that method G calls the basic modulo function and division function, not only function calls, but also a lot of assembly code and registers involved in the operation; while method H is only a few related assembly lines, the code is more concise and efficient higher. Of course, due to different compilers, there may be little difference in efficiency, but, with MS C I have encountered so far, From the perspective of ARM C, the efficiency gap is still not small. The relevant assembly code is not listed here.
</p> </p> <p> <p> Use this trick to pay attention to the problems caused by different CPUs. For example, a program written in this way on a PC and debugged on the PC may cause code hidden dangers when it is ported to a 16-bit computer platform. Therefore, this trick can only be used on the basis of certain advanced technology. </p> </p>
<p> <p>The fourth trick: Assemble embedding</p> </p>
<p> <p>The nirvana of efficient C language programming, the fourth trick—&mdash ; Embedded assembly. </p> </p>
<p> <p>“In the eyes of people familiar with assembly language, programs written in C language are garbage”. Although this statement is a bit extreme, it has its truth. Assembly language is the most efficient computer language, but it is impossible to rely on it to write an operating system, right? Therefore, in order to obtain the high efficiency of the program, we have to use a workaround method - embedded assembly, mixed programming. </p> </p>
<p> <p> For example, as follows, assigning array 1 to array 2 requires that every byte match. </p> </p>
<p> <pre class="brush:<a href="http://www.cppentry.com/list.php?fid=76"> JAVA</a>;"></p>
<p>char string1[1024],string2[1024];</pre> </p>




<p>for (I =0 ;I&lt;1024;I++)</p>
<p> </p>
<p>*(string2 + I) = *(string1 + I)</pre> </p>
<p> <p>方法J</p> </p>
<p> <pre class="brush:<a href="http://www.cppentry.com/list.php?fid=76">JAVA</a>;"></p>
<p>#ifdef _PC_</p>
<p> </p>
<p>int I;</p>
<p> </p>
<p>for (I =0 ;I&lt;1024;I++)</p>
<p> </p>
<p>*(string2 + I) = *(string1 + I);</p>
<p> </p>
<p>#else</p>
<p> </p>
<p>#ifdef _ARM_</p>
<p> </p>
<p>__asm</p>
<p> </p>
<p>{</p>
<p> </p>
<p>MOV R0,string1</p>
<p> </p>
<p>MOV R1,string2</p>
<p> </p>
<p>MOV R2,#0</p>
<p> </p>
<p>loop:</p>
<p> </p>
<p>LDMIA R0!, [R3-R11]</p>
<p> </p>
<p>STMIA R1!, [R3-R11]</p>
<p> </p>
<p>ADD R2,R2,#8</p>
<p> </p>
<p>CMP R2, #400</p>
<p> </p>
<p>BNE loop</p>
<p> </p>
<p>}</p>
<p> </p>
<p>#endif</pre> </p>
<p> <p>Method I is the most common method, using 1024 loops; Method J is differentiated according to different platforms. Under the ARM platform, the same method is completed with only 128 loops using embedded assembly. operation. Some friends here will say, why not use the standard memory copy function? This is because the source data may contain bytes whose data is 0. In this case, the standard library function will end prematurely and will not complete the operation we require. This routine is typically used in the copying process of LCD data. According to different CPUs, skilled use of the corresponding embedded assembly can greatly improve the efficiency of program execution. </p> </p>
<p> <p> Although it is a nirvana, it will pay a heavy price if used easily. This is because the use of embedded assembly limits the portability of the program, which makes the process of porting the program on different platforms, crouching tiger, hiding dragon, and danger! At the same time, this trick also goes against the idea of ​​modern software engineering. It can only be used under unavoidable circumstances. Remember, remember. </p> </p>
<p></div></p></div>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326368174&siteId=291194637
Recommended