资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
第4页 / 共10页
第5页 / 共10页
第6页 / 共10页
第7页 / 共10页
第8页 / 共10页
第9页 / 共10页
第10页 / 共10页
亲,该文档总共10页全部预览完了,如果喜欢就下载吧!
资源描述
如何编写高效简洁的c语言(How to write efficient and concise C language)Introduction:The goal of many software engineers is to write efficient and concise C language code. In this paper, the work of some experience and experience to do related elaboration, not the place, please advise.The first trick: take space for timeThe biggest contradiction in a computer program is a contradiction, space and time then, starting from the angle of reverse thinking to consider the efficiency of the program, we have to solve the problem of the first strokes - a space for time.For example, the assignment of strings.Method A, the usual way:#define LEN 32Char string1 LEN;Memset (string1,0, LEN);Strcpy (string1, This is a example!);Method B:Const char string2LEN = This is a example!;Char * cp;CP = string2;It can be operated directly with pointer when using. )From the above example, we can see that the efficiency of A and B is not comparable. In the same storage space, B can operate directly with pointers, and A needs to call two character functions to complete. The weakness of B is flexibility, not A. When you need to change a string content frequently, A has better flexibility; if using the B method, you need to deposit many strings, although occupy a large amount of memory, but the program execution efficiency.If the system has high real-time requirements and memory, then I recommend you use this trick.The trick of this trick is to use macros instead of functions. Examples are as follows: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 #)#define 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 function and macro function is that the macro function takes up a lot of space, and the function takes up time. You need to know is that the function call is to use the system stack to save the data, if there is a stack check compiler options, general function in head embedded assembler statements to check the current stack; at the same time, CPU also want to save and restore the current scene in the call, to push and play stack operation, therefore, the function call CPU need some time. And the macro function doesnt have this problem. The macro function is only embedded in the current program as a pre written code, and does not generate function calls, so it only takes up space, and this phenomenon is especially prominent when calling the same macro function frequently.The D method is the best placement operation Ive ever seen. Its part of ARMs source code, and implements a lot of functionality in just three lines, covering almost all of the bit manipulation functions. The C method is a variant, and the taste needs to be carefully understood.The second trick: mathematical methods to solve problemsNow, were going to do the second trick of efficient C language - using mathematical methods to solve problems.Mathematics is the mother of the computer, there is no mathematical basis and basis, there will be no computer development, so when writing the program,Some mathematical methods will improve the efficiency of the program by an order of magnitude.For example, seek the sum of 1100.Method EInt I, j;For (I = 1; I=100; I + +) J = I;Method FInt I;I = (100 * (1+100) / 2This example is one of my most impressive mathematical use cases, is my computer enlightenment teacher test me. I was only in grade three of primary school. Unfortunately, I didnt know how to solve the problem with the formula N x (N+1) / 2. Method E solves the problem only 100 times, that is to say, it uses 100 assignments, 100 judgments, 200 additions (I and j), while the method F only uses 1 additions, 1 multiplications, and 1 divisions. The effect naturally speaks for itself. So now, when Im programming, Im trying to figure out the rules and maximize the power of math to improve the efficiency of the program.The third trick: use bit operationThe realization of efficient C language third - use bit operation, reduce the division and modulo operation.In a computer program, the bit of data is the smallest unit of data that can be operated. In theory, bit operations can be used to complete all operations and operations. The general bit operation is used to control the har
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号