Premature optimization is the source of all evil, and 7 Principles of fine number optimization

Source: Internet
Author: User
Code Optimization has many benefits, but it does not mean that all code needs to be optimized. sometimes excessive optimization is counterproductive. DonaldKnuth, the originator of modern computer science, once said that early optimization is the source of all evil, because: making the right program faster... "> <LINKhref =" http://www.php100.com//sta

Code Optimization has many benefits, but it does not mean that all code needs to be optimized. sometimes excessive optimization is counterproductive-time-consuming, laborious, and thankless.

"The originator of modern computer science," Donald Knuth once said, "premature optimization is the source of all evil," because:Making the right program faster is much easier than making the FAST program correct.

In project development, there is always a waste of valuable time for programmers to improve the code that does not need to be improved, without increasing the value through the improvements. When optimizing the project, which areas should be optimized, how should be optimized, and what should not be optimized? You need to first understand the seven things mentioned in this article.





1. what is optimization?

At the beginning of the optimization work, you have not clearly defined the content and purpose of the optimization, so you are easy to fall into misunderstanding. At the beginning, you should clearly understand the effect you want to achieve and other optimization-related problems. These goals need to be clearly stated (at least technically proficient project managers can understand and express it). Next, you need to stick to these goals throughout the optimization process.

In actual project development, various variables often exist. You may need to optimize this aspect at the beginning, and then you may find that you need to optimize the other aspect. In this case, you need to have a clear understanding of these changes and make sure that everyone in the team understands that the goal has changed.

2. select a correct optimization indicator

Choosing the right indicator is an important part of optimization. you need to follow these indicators to measure the progress of optimization. Your efforts may be wasted if you choose an inappropriate indicator or make it completely wrong.

Even if the indicators are correct, some identification is required. In some cases, it is a practical strategy to put the most effort into the code that consumes the most time. But remember that the Unix/Linux kernel spends most of its time on an empty loop.

It is important to note that if you select a metric that is easy to achieve, this does not really help, because it does not really solve the problem. You need to select an indicator that is more complex and closer to your target.

3. Optimization on cutting edge

This is the key to effective optimization. Find the places in your project that are aligned with your goals (performance, resources, or others) and use your effort and time there.

A typical example is that a Web project is slow. Developers focus most of their efforts on database optimization during optimization and eventually find that the real problem is slow network connection.

In addition, do not be distracted from problems that are easy to implement. Although these problems are easy to solve, they may not be necessary or inconsistent with your goals. Easy optimization does not mean it is worth your effort.

4. the higher the optimization level, the better. 

In general, the higher the optimization level, the more effective it will be. According to this standard, the best optimization is to find a more effective algorithm.

For example, in a software development project, the performance of an important application is poor, so the development team began to optimize it, but the performance was not improved much. later, the project staff alternate, when checking the code, new developers found that the core of the performance problem was the addition of thousands of items due to the use of the bubble sorting algorithm in the table.

Even so, high-level optimization is not "silver bullet ". Some basic technologies, such as moving everything out of the loop statement, can also produce some optimization results. Under normal circumstances, a large number of low-level optimizations can produce results equivalent to a high-level optimization.

It should also be noted that high-level optimization reduces some code blocks, so the optimization you have made to these code blocks is meaningless. Therefore, you should consider high-level optimization at the beginning.

5. do not optimize it too early

Optimization is performed early in the project, which may make your code hard to read or affect the operation. On the other hand, in the later stage of the project, you may find that the previous optimization did not play any role and wasted time and energy.

The correct method is to take project development and optimization as two independent steps.

6. Relying on performance analysis, rather than intuition

You often think that you already know where optimization is needed, which is not desirable. especially in complex software systems, performance analysis data should be the first, and finally intuition.

An effective optimization strategy is to sort the optimization results based on the impact of your work. Find the most influential "roadblock" before starting work, and then deal with the small "roadblock ".

7. the optimization is not Wanjin oil

One of the most important rules for optimization is that you cannot optimize everything, or even optimize two problems at the same time. For example, optimizing the speed may increase resource utilization; optimizing the storage utilization may slow down elsewhere. You need to weigh which one meets your optimization goal.

Code Optimization as a Double-Edged Sword

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.