如何编写高效简洁的c语言(how to write efficient and concise c language)

上传人:xins****2008 文档编号:115399384 上传时间:2019-11-13 格式:DOC 页数:10 大小:20.81KB
返回 下载 相关 举报
如何编写高效简洁的c语言(how to write efficient and concise c language)_第1页
第1页 / 共10页
如何编写高效简洁的c语言(how to write efficient and concise c language)_第2页
第2页 / 共10页
如何编写高效简洁的c语言(how to write efficient and concise c language)_第3页
第3页 / 共10页
如何编写高效简洁的c语言(how to write efficient and concise c language)_第4页
第4页 / 共10页
如何编写高效简洁的c语言(how to write efficient and concise c language)_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《如何编写高效简洁的c语言(how to write efficient and concise c language)》由会员分享,可在线阅读,更多相关《如何编写高效简洁的c语言(how to write efficient and concise c language)(10页珍藏版)》请在金锄头文库上搜索。

1、如何编写高效简洁的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

2、 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

3、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 an

4、d 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

5、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 b

6、wMCDR2_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#de

7、fine 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

8、 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

9、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 progra

10、m 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 func

11、tionality 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

12、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

13、 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

14、 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 programm

15、ing, 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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号