C language efficient programming of a few strokes (absolutely practical, absolutely classic)

Source: Internet
Author: User

Writing efficient and concise C language code is a goal that many software engineers pursue. Nonsense don't say, walk up!

The first move: space change time

The biggest contradiction in computer programs is the contradiction between space and time, then, from this point of view, the inverse thinking to consider the efficiency of the program

eg. assigning values to Strings

Method A  usually means a # define LEN 32char String1[len];memset (String1,0,len); strcpy (string1, "This is an example!!"); /method Bconst Char string2[len]= "This was an example"; Char *cp;cp=string2;//(can be manipulated by pointers when used)

As can be seen from the above example, the efficiency of A and B is not comparable, in the same storage space, b directly with the pointer can be manipulated, and a need to call two character function to complete; B's disadvantage is that flexibility is not a good, when you need to change a string content frequently, A has better flexibility If you take method B, you need to pre-store many strings, although it takes up a lot of memory, but you get the high efficiency of program execution.

If the real-time requirements of the system are high and there is some memory, then I recommend using this trick

The trick's side strokes, using the macro function instead of the function, the completion of the I/O GetChar (), the character test sdigit is to get the view of the recognition of the example, the most fundamental reason is the implementation of efficiency, the macro can avoid the overhead of function calls.

Method C#define bwmcdr2_address 4#define bsmcdr2_address 17int bit_mask (int _bf) {    return ((iu<< (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) (((iu<< (bw## _bf)-1)    << (bs# #_bf)) # Define Set_bits (_dst,_bf,_val) ((_DST) &~ (Bit_mask (_BF))) |     ((_val) << (bs# #_bf) & (Bit_mask (_BF)))) Set_bits (Mcdr2,mcdr2_address,registernumber);

The difference between a macro function and a function is that the macro function takes up a lot of space and the function takes up time, you know, the function call is to use the system stack to save the data, if the compiler has a stack check option, generally in the head of the function will sneak into some assembly statements to check the current stack; The CPU also saves and restores the current scene at function calls, pushes stacks and stacks, so the function calls require some CPU events, and the macro function does not have this problem. Macro functions are only embedded in the current program as pre-written code, and do not produce function calls, so it is only occupied space, when the same macro function is called frequently, the phenomenon is particularly prominent.

D method is I see the best position operation function, is arm company source code part, in a short three lines to achieve a lot of functions, almost covers all the bit operation function, C method is its variant, which taste also need to be carefully experienced!!

The 2nd trick: Solving problems with mathematical methods

Now we deduce the second trick of efficient C language---to solve the problem by mathematical method.

Mathematics is the mother of computers, there is no basis for mathematics and the foundation, there is no computer development, so in the programming process, the use of a number of mathematical methods will be the implementation of the program to improve the efficiency of the order of magnitude.

Seeking 1~100 and

Method Eint I,j=0;for (i=0;i<100;i++) {    j=j+i;} Method Fi= (100* (1+100))/2

Obviously, the effect is naturally self-evident, so now we are compiling programs, more is the use of ideas to find the law, maximize the power of mathematics to improve the efficiency of the program operation.

3rd trick: Using bit manipulation

The third trick to achieve efficient C language----use bit manipulation to reduce division and modulo operations.

In computer programs, bits of data are the smallest units of data that can be manipulated, and in theory, "bitwise operations" can be used to complete all operations and operations. The general bit operation is used to control the hardware, or to do the data transformation use, but the flexible bit operation can effectively improve the efficiency of the program operation.

Methods: Gint I,JI=257/8; j=456%32;//Method: Hint i,ji=257>>3; j=456-(456>>4>>4);

It's literally like H is a lot more trouble than G, but a closer look at the resulting assembly code will make it clear that method G calls the basic modulo and division functions, both function calls, and many assembler and register participation operations, while Method H is just a very relevant compilation, the code is more concise and more efficient. Of course, due to the different compilers, the efficiency gap may be small.

The use of this trick needs to be noted , because of the CPU's different problems, for example, the program written on the PC, and debugging on the PC through, in a 16-bit machine on the platform, can generate code hidden trouble, Therefore, it is only possible to use this trick on the basis of the advanced technology.

The use of C language for efficient programming, my experience is only this, I also invite you to learn together, I hope you can give a better way, we together improve our programming skills.

Transferred from: http://www.cnblogs.com/haoyuanyuan/p/3234200.html

C language efficient programming of a few strokes (absolutely practical, absolutely classic)

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.