Code Daquan 2 study notes 4

Source: Internet
Author: User

Part V: Code improvement

25 Code Tuning Policy

In the 70 's, programmers realized that overly focused performance would lose the readability and maintainability of the program.

For ordinary users, programmers deliver software on time, a refreshing interface, and avoid crashes, often more important than performance.

Consider several issues before optimizing efficiency:

1, the requirements of the program (customer demand is too high, in fact, there is no need so high)

2, the design of the program (the current design is difficult to do a good job of optimization)

3, the specific design (such as the use of quick sorting or bubble sort)

4, the interaction with the operating system (affecting your efficiency, sometimes you do not mean to have a system call, such as the compiler will generate system calls, or a library used)

5, code compilation (good compiler will optimize for you)

6. Hardware

7. Code adjustment (Chapter theme, detail code)

"Less than 4% of the parts often occupy more than 50% of the running time"

1, "Number of lines of code, does not mean that the speed is slow"

for (int i=0;i<10;i++)
A[i]=i;

And
a[0]=1;a[2]=2;......a[9]=9;

Compared to the obvious is the back one faster.

2, "Optimization to consider portability, perhaps one way to put in another language, and even another compiler in the same language, can have a reaction." ”

3, "It's hard to find the 4% part, the programmer does the optimization often brings the effect is insignificant." ”

4, "To that 4% optimization, may forgotten how, but lead to the overall performance of the decline." ”

5, "Speed optimization will bring readability, encapsulation, correctness of the damage, the benefits are not worth it." ”

6, "The requirements of the speed of operation is now increasingly low and correct and so on." ”

7, "programmers should use high-quality design, the code is written correctly, so that it is modular and easy to modify, will make later maintenance work easy." ”

8. "Compiler optimized Open will provide performance"
The C + + compiler opens optimizations that basically improve performance by approximately 50%. You can refer to this example:
Http://topic.csdn.net/u/20091113/14/9d793a37-9953-43be-940f-26e4b64d8a6b.html
The optimization is very aggressive.

9. "System Impact"
(1) Access to memory is faster than access to the network and disk.
(2) System call
(3) Improper allocation of memory data, resulting in memory paging can also affect performance:
for (int column=0;column<100;column++)
for (int row=0;row<1000000;row++)
Table[row][column] = new XXX;
Because reading data is generally read, and then each column is fetched, the allocation scheme causes the memory page to be switched on the read (pages break), which affects performance.
So it should be:
for (int row=0;row<1000000;row++)
for (int column=0;column<100;column++)
Table[row][column] = new XXX;
However, this can cause the local run to slow down, see chapter below.

10. The more parameters of C + + function, the slower the call speed. "It's all about the basic data type, not the structure."
It's amazing. The number of function parameters in Java does not affect the speed of operation, it should be optimized.
polymorphic function calls are the slowest.
11, there is no need to move the array into a multidimensional pointer to operate, because the compiler is basically optimized for us, if we do again, will cause readability.

26 Code Tuning Techniques

"The reduction in resource usage is mainly achieved by adjusting classes and data structures rather than code adjustments. ”

1, Case statement, If=else first Judge Common.

2. Replace complex logic with table-checking method.

3. The calculated structure can be placed in the cache for the next use.

4, the judgment in the cycle of the outside to mention
for (int i=0;i<count;i++)
{
if (type=a)
Xxxx
Else
Xxx
}
Become
if (type=a)
for (int i=0;i<count;i++)
Xxx
Else
for (int i=0;i<count;i++)
Xxx
But then there is the duplicate code, which is a problem for maintaining the code.

5, expand
for (int i=0;i<count-2;i++)
 a[i]=i;
Change to
for (int i=0;i<count;i++)
{
 a[i]=i;
 a[i]=++i;
 a[i]=++i;
}
xxxx//processing additional data
is basically considered bad programming, but if it is simple to handle, it can provide 34% efficiency.
"The sense of meaning is not significant, and if the internal processing of the loop is time consuming, this improvement is diluted"

6, reduce the internal processing of the cycle
for (int i=0;i<count;i++)
a[i]=b[i]*p->p->p;
Become
pr=p->p->p;
for (int i=0;i<count;i++)
A[I]=B[I]*PR;
7, Sentinel value "This feeling is more perverted, the first time to see such a strange way."
for (int i=0;i<count;i++)
if (Item[i]==type)
{bfind=true;break;}
Change into
Item[count]=type;
while (Item[i]!=type)
i++;
if (I<count)
Xxxx
It's so weird, it's less of a judging condition.
8. Put the busiest loop in the inner layer
Observing the previous Chapter 9, if you do not consider allocating memory, then the first loop is finally 1000000*101 times, and the second is 1000001*100, 999,900 less cycles.
But although the local optimization, the overall decline.
It would be nice if you didn't allocate memory but simply handle it.

9. Substituting multiplication with addition

10, the circular subscript does not use the floating point type and the integral type "the efficiency increases 3.5 times times, but the normal person should not use the floating-point type to do the circular circulation subscript bar"

11, with one-dimensional arrays instead of multidimensional arrays, there is no efficiency improvement, the compiler has been optimized.

12, the common calculation results into a constant.
such as int log2 (int x) {return log (x)/log (2);}
Turn log (2) into constant use.

13. Be careful with system functions
For functions that do not require precision, write to them directly.

14. Use the correct constant type
When assigning a variable, the given value needs to be converted, which consumes time because it is cast. "Free Code to try"

15, calculate the results in advance, directly check the table

16. The procedure becomes an inline function

17, with low-level language rewrite code "difficult to change to the Assembly, especially the more complex procedures, with the Assembly rewrite is too difficult--estimated to write a few algorithms can also, but do not know how to call the assembly program, is it a standalone program call return results?" Or is the compiler able to call the assembly as part of the program? TC has a small mode that can call assembly code. ASM File "
Try this code when you are free.
Http://zhidao.baidu.com/question/31397560.html

Tried the Linux version of the card is dead, nothing print out.

The compilation on Linux is difficult to understand, and it is not the same as it was before school.

g++-S can generate assembly code.

There are some more characters in the spelling. Some instructions are followed by an L, the source operand and the destination operand position are swapped, the constant is preceded by $, and the register is preceded by a%

http://tieba.baidu.com/f?kz=310375556

Http://wenku.baidu.com/view/e8752e7f5acfa1c7aa00cc39.html

You can have a look when you are free.

Also a simple example:
int a = 1;
int b = 2;

int main ()
{
__asm__
(
"Movl a,%ecx \ n"
"Xchg b,%ecx \ n"
"Movl%ecx,a \ n"
);
return 0;
}

Variables must be defined in all, not with static.

But in general now in the code inside the assembly meaning is not big, see this article
Http://topic.csdn.net/u/20100413/20/87aa7378-cd9f-44ad-90ae-4201a3680b71.html


This article is from "Flying Justice Blog" blog, please be sure to keep this source http://xzq2000.blog.51cto.com/2487359/1767393

Code Daquan 2 study notes 4

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.