Why Premature Optimization Is the Root of All Evil

Why premature optimization is the root of all evil?

origin

Donald Knuth is a well-known scholar in computer science and one of the pioneers of computer programming. He is known as the "Bible" of computer science and the author of "Computer Programming Art" , he proposed the famous "big O notation" to describe the time complexity and space complexity of algorithms, and developed the TeX system for typesetting scientific and technological documents. He has won many honors such as the Turing Award, the Von Neumann Award, and the National Medal of Science. What I want to talk about today is an important principle of software design that he put forward. Premature optimization is the root of all evil. Premature optimization is the root of all evil.

Premature optimization is the root of all evil

Why is it said that "premature optimization is the root of all evil"?

I think optimizing code too early can lead to getting caught up in the wrong goals and losing sight of the most important goals. To give a very simple example, you need to quickly build a product to grab users. Your most important goal right now is to get this product online quickly , not to make this product easy to use (under the Chinese Internet, there are countless such things) , if you only pay attention to the latter experience and performance issues and ignore the speed, in the current highly competitive market, you have no chance at all.

Of course, the above example is from the perceptual level, and for many programmers, it may not involve the content of the product level. From the perspective of software design, rationally speaking, premature optimization may lead to the following problems:

1. Increased code complexity : Excessive optimization may lead to increased code complexity, which reduces code readability and maintainability. If the code is too complex, it may make it difficult for developers to understand and maintain the code, which increases development costs and time.
2. Consuming development time and resources : Excessive optimization may cause developers to spend a lot of time and resources on code performance optimization, while ignoring other important development tasks. This can lead to project schedule delays and increased development costs.
3. Reduced code portability : Excessive optimization may lead to reduced code portability. If the code is too dependent on a particular hardware or operating system, it may prevent the code from running in other environments.
4. Reduce the scalability of the code : Excessive optimization may reduce the scalability of the code. If the code is too dependent on a specific algorithm or data structure, it may cause the code to be unable to adapt to future changes in requirements.

Typical Cases of Premature Optimization

In the history of software engineering, there are many cases of project failure due to excessive focus on software performance. For example, some of the projects I will talk about below are very well-known projects in the history of software engineering (of course, some new generation programmers may not know it anymore. ).

1. IBM OS/360 operating system : In the 1960s, IBM Corporation developed the OS/360 operating system, which was one of the largest software engineering projects of its time. During the development process, IBM paid too much attention to the performance of the code, resulting in an increase in the complexity of the code, a delay in development time, and ultimately the failure of the project. I know that this project is still in a classic software engineering book "The Mythical Man-Month" that I was reading recently. I also recommend you to read it. Although this project failed in the end, it also left valuable experience for the entire software engineering field. .
2. Netscape Navigator browser : In the 1990s, Netscape Corporation developed the Navigator browser, which was one of the most popular browsers at the time. During the development process, Netscape paid too much attention to the performance of the code, resulting in an increase in the complexity of the code, a delay in development time, and ultimately a serious decline in the browser market share.
3. Windows Vista operating system : In the early 2000s, Microsoft Corporation developed the Windows Vista operating system, which was one of the largest software engineering projects at the time. During the development process, Microsoft paid too much attention to the performance of the code, resulting in increased code complexity and delays in development time, which eventually led to poor user experience of the operating system and poor market response. By the way, I have used this operating system before, the user interface is still very beautiful, and many UI designs have also been used in Window7.

How to Identify Premature Optimization

In the software development process, how to judge whether it is premature optimization? Here are some general judgment criteria, you can simply refer to the following:

1. Whether there are performance problems: If the code has no performance problems, then premature optimization is unnecessary. Therefore, before optimizing, you should test the performance of the code to determine whether there are performance problems.
2. Whether the problems that may occur in the future are optimized : If the problems that may occur in the future are optimized instead of the current problems, then it may be premature optimization. Priority should be given to current problems rather than possible future problems before optimizing.
3. Whether the readability and maintainability of the code are sacrificed : If optimizing the code will increase the complexity of the code and reduce the readability and maintainability of the code, then it may be premature optimization. Code readability, maintainability, and scalability should be prioritized before optimization.
4. Whether a lot of development time and resources are wasted : If optimizing the code will waste a lot of development time and resources instead of improving the performance and efficiency of the code, then it may be premature optimization. Before optimizing, you should evaluate the costs and benefits of optimization to determine whether it is worthwhile.

Judging whether to optimize prematurely needs to be evaluated on a case-by-case basis. Before optimizing, you should test the performance of the code to determine whether there are performance problems. At the same time, the readability, maintainability and scalability of the code should also be prioritized to avoid over-optimization.

Summarize

As an engineer who has worked in the IT field for many years, I deeply understand that premature optimization is a big trap in software development. In the early stages of software development, we may pay too much attention to the performance of the code, while ignoring the readability, maintainability and scalability of the code. This approach may lead to increased code complexity, reduce code readability and maintainability, and may even waste a lot of development time and resources.

During software development, we should avoid premature optimization and instead prioritize code readability, maintainability, and scalability. When performance optimization is required, it should be optimized on the basis of the code, and the performance and efficiency of the code can be improved by analyzing performance bottlenecks, optimizing algorithms, and data structures. At the same time, we should also realize that performance optimization is not the only goal of software development, we should also pay attention to the readability, maintainability and scalability of the code in order to ensure the quality and reliability of the code.

expand

There are many benefits of code optimization, but this does not mean that all code needs to be optimized. Sometimes excessive optimization is counterproductive-time-consuming, labor-intensive, and unpleasant.

"The originator of modern computer science" Donald Knuth once said that "premature optimization is the root of all evil", because: it is much easier to make the correct program faster than to make the fast program correct.

In project development, there are always programmers who waste valuable time improving code that does not need to be improved, without adding value through the improvements made. When optimizing a project, what should be optimized, how should it be optimized, and what should not be optimized? You need to understand the 7 things mentioned in this article first.

1. What exactly should be optimized?

At the beginning of the optimization work, you have not yet clearly defined the content and purpose of optimization, so you can easily fall into misunderstandings. From the very beginning, you should have a clear understanding of the effect you want to achieve, as well as various other optimization-related issues. These goals need to be clearly stated (at least a technically savvy project manager can understand and express it), and then you need to stick to these goals throughout the optimization process.

In actual project development, there are often various variables. You may start by optimizing one aspect, and later you may find that you need to optimize another aspect. In this case, you need to be clear about the changes and make sure everyone on the team understands that the goals have changed.

2. Choose the right optimization metric

Choosing the right metrics is an important part of optimization, and you need to measure the progress of your optimization work against these metrics. If the indicator is not chosen properly, or simply wrong, your efforts may be wasted.

Even with the right metrics, there has to be some discernment. In some cases, it's a practical strategy to put the most effort into running the parts of the code that take the most time to run. But also keep in mind that most of the Unix/Linux kernel's time is spent in empty loops.

It should be noted that if you lightly choose an indicator that is easy to achieve, this will not help much, because it will not really solve the problem. It is necessary for you to choose a more complex indicator that is closer to your goal.

3. Optimization is on the cutting edge

This is the key to effective optimization. Find the places in the project that work against your goals (performance, resource or otherwise) and spend your effort and time there.

To give a typical example, the speed of a web project is relatively slow, and the developer puts most of his energy on database optimization when optimizing, and finally finds that the real problem is the slow network connection.

Also, don't get distracted by low-hanging fruit. These issues, while easy to address, may not be necessary or consistent with your goals. Just because it's easy to optimize doesn't mean it's worth the effort.

4. The higher the optimization level, the better

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

For example, in a software development project, there was an important application with poor performance, so the development team started to optimize, but the performance did not improve much. It was found that the core of the performance problem was due to the use of the bubble sort algorithm in the table, resulting in an increase of thousands of items.

Still, high-level optimization is not a "silver bullet". Some basic techniques, such as moving everything outside the loop statement, can also produce some optimization effects. Often, a large number of low-level optimizations can produce the same effect as one high-level optimization.

It should also be noted that high-level optimization will reduce some code blocks, so the optimization you made on these code blocks before will have no meaning. Therefore, high-level optimization should be considered from the beginning.

5. Don’t optimize prematurely

Optimizing early in the project can make your code hard to read, or it can affect performance. On the other hand, at the later stage of the project, you may find that the optimizations done before have not played any role, wasting time and energy in vain.

The correct way is that you should do project development and optimization as two separate steps.

6. Rely on profiling, not intuition

You tend to think that you already know where you need to optimize, which is not advisable, especially in complex software systems, profiling data should come first, and intuition last.

An effective strategy for optimization is to rank the work you are doing according to its effect on the optimization. Find the most impactful "roadblock" before starting work, and tackle the smaller ones later.

7. Optimization is not a panacea

One of the most important rules of optimization is that you can't optimize everything, not even two problems at the same time. For example, optimizing speed may increase resource utilization; optimizing storage utilization may slow down other places. You need to weigh, which is more in line with your optimization goals.

Guess you like

Origin blog.csdn.net/heshihu2019/article/details/132611086