Two basic code optimization technologies that are most easily overlooked

Source: Internet
Author: User

Dr. Dobb's blogger
Walter bright wrote a blog post titled 《


Overlooked essential tials for optimizing code


And summarizes two basic code optimization technologies that are most easily overlooked. Cool Shell
Personal website moderator Chen Hao

Translated this article
.


The full text is as follows:

I have been programming for 35 years, and I have done a lot of work on program execution speed optimization (an example

), I have also seen optimizations made by others. I found that two basic optimization technologies are always ignored.

Note that these two technologies are not optimized to avoid time immaturity. It does not change Bubble sorting to fast sorting (algorithm optimization ). It is not a language or compiler optimization. It is not optimized to write I * 4 as I <2.

The two technologies are:

Use a profiler.

View the assembly code when the program is executed.

Those who use these two technologies will successfully write the code that runs fast, and those who do not use these two technologies will not. Let me explain it for you.

Use a profiler

We know that 90% of the running time is spent on 10% of the Code. I found this was not accurate. Again and again, I found that almost all programs will spend 1% of the Code
. However, Which is 1%? A good profiler can tell you the answer. Even if we need to use 100 hours to optimize the 1% code, we can use 100
Optimization on the other 99% of the Code produces much more benefits.

What is the problem? Do people use profiler? No. I used a gorgeous and luxurious profiler in one of my jobs, but since I purchased this profiler,
Its packaging has been so new for three years. Why don't people use it? I really don't know. Once, my colleagues and I went to an exchange with a high load. My colleagues insisted that he knew where the bottleneck was. After all, he
Is a very experienced expert. Finally, I ran my profiler on his project, and we found that the bottleneck was totally unexpected.

Just like a racing car. The team is winning in sensors and logs, and these things provide everything. You can adjust the trousers of a racing driver to make it more comfortable during the competition, but it won't win you
The competition will not make you more competitive. If you do not know that your speed is not going because of an engine, exhaust device, empty body dynamics, tire pressure, or a racing driver, you will not be able to win. Why is programming different?
What about it? Without measurement, you will never make progress.

There are too many profiler available in the world. You can find the call level, number of calls, and time breakdown table of each previous Code (or even
Editing level ). I have seen many programmers shy away from using profiler. Instead, they spend their time "Optimizing" in useless and wrong directions and are humiliated by their competitors. (Translator Chen Hao note
: When Using profiler, focus on the following: 1) a function that takes a long time to optimize its algorithm; 2) a function with a large number of calls-if a function is called times per second, you only need to optimize it by 0.001 milliseconds, which is also a great optimization. This is what the author calls 1% of the Code taking up 99% of the CPU time)

View assembly code

A few years ago, I had a colleague, Mary Bailey, who taught correction algebra (remedial algebra) at the University of Washington. Once, she wrote on the blackboard:

x + 3 = 5

Then ask his student "solve X", and the student does not know the answer. So she wrote:

__ + 3 = 5

Then, ask the students to fill in the blanks. All the students can answer the question. The unknown number X is like a magic letter that makes everyone think, "X means algebra, and I have never learned algebra, so I don't know how to do it ".

Assembler is the algebra of the programming world. If someone asks me, "Is the inline function expanded by the compiler ?" Or ask me, "if I write I * 4, the compiler will optimize it to the left shift operation.
?". At this time, I suggest them look at the assembly code of the compiler. Is this answer rude and useless? Generally, after I answer the questioner in this way, I usually say, sorry, I don't know.
What is assembly! Even C ++ experts will answer this question.

Assembly language is the simplest programming language (even compared with C ++), such:

ADD ESI,x

(C-style code)

ESI += x;

And:

CALL foo

Then:

foo();

The details vary depending on the type of CPU, but that's how it works. Sometimes, we don't even need details. Just look at the length of the assembly code, and then compare it with the source code, you can know that there are a lot of assembly code.

So how can this help code optimization? For example, I met a programmer a few years ago and thought that he should discover a new and faster algorithm. He has a benchmark to prove this algorithm,
He wrote a very beautiful article about his algorithm. However, someone looked at the original algorithm and the new algorithm assembly, and found that his improved version of the algorithm allows its compiler to change the two Division operations
. This has nothing to do with algorithms. We know that Division operations are very expensive, and in their algorithms, the Division operations are still in an embedded loop.
Of course it should be faster. However, you only need to make a small change on the original algorithm-using a division operation, the original algorithm will be as fast as the new one. His new discoveries are nothing.

In the next example, a D user posts a benchmark to display
Compiler) is very bad in integer algorithms, while LDC (llvm D compiler)
A lot. I have some comments on this result. I quickly looked at the assembly and found that the two compilers are quite consistent. There is no obvious difference between the two compilers.
Responsible. However, we can see that there is a division for long integers, which calls the Runtime Library. This database becomes the killer of time consumption, and all other addition and subtraction operations have no effect on speed. Unexpected
Location, Benchmark
It has nothing to do with algorithm code generation. It is completely a long integer division problem. This exposes the implementation of long division in the running library of the DMD system. After modification, the speed can be improved. Institute
This has nothing to do with the compiler, but if you don't read the assembly, you won't be able to find it all.

Checking the assembly code often gives you some unexpected things that let you know why the program's performance is like that. Some unexpected function calls, unexpected pride, and things that should not exist are all in fact. But it does not need to be a hacker who assembles code.

Conclusion

If you think the program needs a better execution speed, the most basic method is to use a profiler and be willing to check its assembly code to find the bottleneck of the program. Only when the bottleneck of the program is found, it is time to really think about how to improve, such as thinking about a better algorithm, using faster language optimization, and so on.

The conventional approach is to select the best algorithm instead of micro-optimization. Although there is no objection to this practice, there are two things that the school has not taught you and you need to pay attention. First
Is also the most important. If your optimized algorithm is not involved in the performance of your program, it is a waste of time and effort to optimize it, it also changes your attention and makes you miss out what you should optimize.
. Second, the total performance of the algorithm is closely related to the data processed. Even if the Bubble Sorting has so many jokes, if the data processed by the algorithm is sorted, only a few of the data are unordered,
So Bubble Sorting is also the best performance in all sorting algorithms. Therefore, it is a waste of time to worry about not using good algorithms rather than measuring, whether it is yours or a computer.

Just like the speed at which racing parts are ordered won't make you more competitive (even if you correctly install the parts). Without profiler, you won't know where the problem is, if you don't look at the assembly, you may know the problem, but you often don't know why.

Original article:Overlooked essential tials for optimizing code


Http://coolshell.cn/articles/2967.html.

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.