Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Planning for efficiency early vs Premature optimization

I seem to notice two schools of thought emerging in regards to optimization:

  1. Premature optimization is the root of all evil. You should only optimize when you've written the most readable and simplest thing possible. If after profiling you determine that the software is too slow, you should optimize.
  2. Optimizations should be done early in a project's lifecycle. Optimizations need to be planned for, but should be done reasonably.

On the face of things, they seem to be fairly opposed viewpoints. The thing is, I see the merit in both schools of thought. I can also think of times when both of these ways of thinking have helped me write better and faster software.

Is there any way to reconcile these two ideas? Is there a middle ground? Is there a time when one idea is the best tool for the job? Or am I presenting a false dichotomy and both views can co-exist peacefully?

like image 618
Jason Baker Avatar asked Feb 05 '09 14:02

Jason Baker


People also ask

Is Premature optimization good?

Premature optimization, at the microscopic level, is usually a bad idea. This does not suggest, however, that engineers should not be concerned about application performance. The fallacy that "premature optimization" is the same thing as "concern about performance" should not guide software development.

How can premature optimization be prevented?

Avoid premature optimization by getting user feedback early and often from your users. If you need help optimizing the performance of your application, be sure to check out our offerings. Prefix can help you find performance problems as you write your code.

What is premature optimization in computer science?

"Premature optimization" is a phrase used to describe a situation where a programmer lets performance considerations affect the design of a piece of code.

Who said premature optimization is the root of all evil?

Although Knuth popularized this concept, it has also been attributed to others, including Tony Hoare and Edsger Dijkstra, and Knuth himself referred to the statement “premature optimization is the root of all evil” as Hoare's dictum in 1989. However, evidence suggests that Knuth is the one who coined the phrase.


1 Answers

Optimise at the design and architecture level early on. Micro-optimise at the implementation level later.

You need to be aware of the performance costs of design decisions you make which will be hard to change later. Implementation can often be tuned later on, which is why it's not worth doing it until you know that it's a problem.

like image 154
Jon Skeet Avatar answered Sep 28 '22 10:09

Jon Skeet