I'm Keyvan Nayyeri, a 28 years old software engineer working at Match.Com and living in Dallas, Texas.
I have a Master’s degree in computer science and a bachelor's degree in applied mathematics. I’m also known to be a technical author with several technical publications in the form of books and articles. Besides, I'm an open source enthusiast and have coordinated or contributed to several projects. Currently, I maintain my projects on GitHub.
As a content provider on the internet, not only I publish on this technical blog, but also I'm a podcaster and publish audio podcasts on Mash This.
Trying to maintain a healthy and active lifestyle, I'm a pescetarianist and exercise almost everyday. I’m an avid runner, soccer defender, and tennis player. I also have an interest in fashion.
Stack Exchange websites and programming forums are a great way to share information specifically about techniques of programming, but as any other phenomena in our world, there is a downside to them when they’re misused. The fact that the questions on these sites are answered mostly by ordinary programmers makes them a bad resource for discovering and learning about concepts and principles (while they work great for techniques and practices). Many of these programmers leave their incorrect understandings of concepts and try to use irrelevant evidence to support their claims. One of these supporting evidence is quotes from famous computer scientists, something that usually works in a wrong way than a right one!
One of the common quotes that is often misused by programmers as an excuse for being sloppy about their code performance is a famous one by Donald Knuth where he uses the term premature optimization.
Premature optimization is the root of all evil.
And no wonder if many programmers quote this short sentence out of a longer one (which is still incorrect as they don’t read the whole thing in the context of the original paper), and apply their own understanding of the raw English meaning of the word premature in there.
Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%.
After writing my post on the performance penalties of DateTime.Now, I had to address this situation with one of these programmers who had the impression that he’s referring to Bible by quoting that short sentence from Stack Overflow, and I also saw the same thing happening on a recent blog post by Scott Hanselman where he used LINQ deferred execution for correcting a code!
It’s so sad to see this happening on the programmers community that is supposed to be open and look for the truth. Such a misleading hint for programmers will have one simple impact on the software being built: some programmers will not build higher quality software! It’s our responsibility as humans to talk about something when we have a minimum knowledge in it. Performance evaluation is a science with certain techniques defined but some people try to get over that by some tricks to prove that an optimization doesn’t work! It’s also the nature of programming and software development to mandate the programmer to be very open-minded and avoid biased views. If somebody has said something somewhere, it’s not a rule, and specifically in computers world, it has a good chance to be wrong.
Luckily, there have been some attempts by other programmers and computer scientists to address this problem, and the latest one was by Reed Copsey, Jr. who wrote a good blog post on it. Even on Programmers Exchange, Scott Dorman has a good response to one of these questions that is worth reading and sharing.
Having this background, I thought that it’s time for me to write my own post and describe several points related to optimization and premature optimization to clarify them because optimization is all I’m doing here at academia and it has taken most of my time in the past two years. I just hope that those programmers out there who tend to copy/paste some quotes on Stack Exchange websites, copy/paste these writings along with them, so the audience can decide what is right and what is wrong!
I can bet that almost all people who use the term premature optimization in their literature are those who never read the original paper where the above sentence is written in even though that paper has a simple text which is easy to follow for all programmers! Knuth wrote a paper in 1974 to talk about structured programming with go to statements that obviously had a totally different focus and its primary intentions are useless in today’s computing because we don’t use GO TO statements in typical programming anymore!
As a part of the manual optimizations that he describes in that paper, he notes that programmers shouldn’t spend their time optimizing a code that is not obvious to be a bottleneck which can take their focus of the design of the program, and he refers to such a case as premature optimization. As I’ll write later, here he’s talking about a particular type of optimization (design-time optimization) and not all of the optimization levels. Besides, he’s clear that it’s a premature optimization if it affects the design of the program significantly which doesn’t apply to many cases where this term is misused by programmers to be sloppy. If you read the paper, you’ll be surprised what types of techniques are introduced in there to make a structured program and optimize its code. It covers techniques that are no longer used by programmers as they’re either removed from programming paradigms, or are hidden in the compilers.
So the first step is to read that paper, the whole thing, before quoting something out of that, and do not forget that the paper is written in 1974 and has a very clear goal regarding GO TO statements.
Unfortunately, most (not all) programmers don’t know a scientific definition for optimization, as they don’t know anything about something very related called performance evaluation! Most programmers believe that optimization is the task of modifying the source code of a program to just run faster which is only a part of the broader definition of optimization. Likewise, performance evaluation is nothing but calculating the execution time of a piece of code for them!
Optimization comes in different levels and forms to improve the efficiency of software and that efficiency should be defined. In a common sense efficiency consists of saving resources including time, CPU, memory, and disk, but it’s usually hard to achieve them altogether, and that’s why efficiency is defined on a per-case basis.
One of the main points missing by programmers is that optimization comes at different levels and in different forms like low-level assembly code, compile-time, or runtime optimizations or higher-level optimizations like those applied to programming codes directly by programmers or even optimizations in design. This last level of optimization is the main one that Knuth is pointing to in his paper as he tries to emphasize that at the design time, we shouldn’t spend too much time and efforts or deviate from the main goal only in order to optimize something.
On the other hand, low-level optimizations like assembly code, compile-time, or runtime optimizations are vital to the execution of a program. In fact, without such optimizations, a reasonable number of the heavy programs written in today’s software world couldn’t run on the current hardware! In such cases, optimization is a need!
It may be interesting to know that in the current research world in compilers a very tiny optimization on a single architecture is the outcome of a research project for months and makes a good publication! We’re at a point where many of these low-level optimizations are the ultimate goal of the researchers.
But what is premature optimization really? Premature optimization has come from that paper by Knuth and is defined in those terms. What he really means by premature optimization is a type of optimization that is done at design time (or during the original coding process) that forces the programmer to take his focus off the design and add an extra complexity to the design that doesn’t add much to the performance. This is very clear from the text of the paper or even from the longer version of the quote that I’ve mentioned above.
However, there are many developers who interpret premature optimization as any optimization applied during the coding process that doesn’t have a big impact. Not only this is a wrong interpretation, but also it allows us to ask some simple questions that can’t be answered clearly by such programmers. One of them is a method to distinguish between a big optimization and a small one because there doesn’t exist a threshold for that.
The term premature optimization is used to kill many good authors by programmers when there isn’t an optimization going on! In fact, what is described to avoid a bad practice is considered an optimization which is totally wrong. Many times, we’re referring to a good or recommended practice that is forgotten and disregarded by programmers, and this is not an optimization.
A good example is my post on DateTime.Now’s performance penalties. This situation is a recommended practice that should be followed and there is nothing about optimizing your code. If you’re using DateTime.Now rather than DateTime.UtcNow in places where it’s not supposed to be used, then you’re doing something wrong, and it’s not an optimization to use DateTime.UtcNow. In these cases, you’re not optimizing anything. What you’re doing is correcting a mistake!
With the rise of virtual machine hybrid compilers like .NET and Java, and the complex transformations that they apply to the original code before running it on the hardware, it’s very important to follow some recommended practices by the builders of the compilers because these practices and recommendations are those that have a close relationship with the internal working of the compiler. These points have nothing to do with optimization. They’re the way things should be used, and they’re like the manual for this system.
Furthermore, even if we consider these circumstances as optimization, we should note that all these examples are not affecting our design. These are very minor changes to code that just make it work the way it’s supposed to work in the first place! These are the type of practices that should be followed at the coding time.
To rant a little bit about some programmers who really hurt the software community by the use of this premature optimization term, let me say that this, along with many other things that we hear from some (but not all) programmers, is just an excuse for sloppiness. Just like the people of the society, there are some professionals who care about many things and live open-minded and try to at least listen to others’ opinions, and there are also some close-minded people who want to stick to their beliefs and not care about what’s going on. You see that on the .NET community, too, where there are some people following ALT.NET to try the best practices, and there are also some people who hardly care about many of the new technologies introduced by Microsoft.
Throwing something like a quote on a valid statement is easy to fool yourself to not correct your mistakes, and there is nothing more than that. Let’s be real and respect other people’s expertise. When somebody with a long history of expertise and knowledge in the field is bothering himself/herself to publish something, there is enough thought behind that for us to at least spend some time thinking about his points.
The term premature optimization is mistakenly interpreted by some programmers and spread on programming forums to be used against many great writings on optimizations and best practices. The term premature optimization, introduced by Donald Knuth in an old paper doesn’t imply anything about not optimizing a code if it’s small. There is no smallness defined in performance evaluation to prevent optimization.
What premature optimization means is an optimization that has a little impact but keeps the programmer or designer busy during the design phase to change the design significantly. This, of course, is very different from many of the circumstances where a recommended practice is not followed or a small change without affecting the design can give us better results.
Zack Owens
Sep 21, 2011 7:06 AM
#
Keyvan Nayyeri
Sep 21, 2011 7:33 AM
#
Thank you for your comment, but let me correct something in there: this is not my *feelings*. This is the truth about premature optimization. If it was just a feeling, it was subjective and couldn't belong to a discipline like computer science :-)
Drew Peterson
Sep 22, 2011 10:20 AM
#
Keyvan Nayyeri
Sep 22, 2011 11:12 AM
#
Thanks for your comment. Yes, your points are 100% true.
Freddy Rios
Sep 22, 2011 3:34 PM
#
But we have to be carefull. You can't discard the concept.
Just like you mentioned, optimizations are necessary. But these are made based on measurements and understanding.
Take a web site that's serving a small niche audience for example. What's better, developers going all over getting gains of 30% of overall processing of requests, or applying other optimizations like script/css minification, reducing requests x page, etc.
The answer of course is it depends. It has to be measured. I've had clients with third party built sites, that had very slow pages where the server responds in 0.5 - 1 seconds, but the rest that happens when the page is loaded took over 10 seconds. I've also had clients with the whole web resources optimizations well done, but the server responding the page requests on 10+ seconds. Clearly both were crying for help (10 seconds is being generous ...).
So, performance needs to be a top consideration all around. But the point of avoiding premature optimization, is to make sure we don't forget the whole picture when we're optimizing and that we do it responsibly. That is Not ignoring performance, which leads to those ugly situations I mentioned above.
At least that's the way I've been using it. And whoever said to hanselman that was premature optimization is crazy (I didn't read the comments, so I mean that if it was just out of the blue and not in reply to any comment in particular). The context at microsoft, is they build the framework, they need to know how to optimize it ... for those % of customers that do have a valid scenario worth optimizing. Of course, there is nothing wrong of exposing the gains for the rest of us, as long as simplicity is kept in the usage.
Keyvan Nayyeri
Sep 22, 2011 5:10 PM
#
Thanks for your comment and sharing your thoughts.
But unfortunately, you're having the same interpretation of the premature optimization that I consider the wrong one because you're making the argument that optimization is subjective based on the scale of the software, so a small site is different from a big one.
This is not correct. Performance is a single measurable concept regardless of scale and has an absolute definition. Should you disregard performance for a small program, it's a sign of sloppiness about a work.
Of course, it's totally arguable that it may not be economic at some points (and I partially agree) but that's a different story. From a software perspective, we don't have anything like subjective or relative performance that varies by size.
Jim Danby
Oct 04, 2011 2:00 PM
#
It also doesn't help your argument to be condescending about "ordinary developers".
Keyvan Nayyeri
Oct 04, 2011 2:17 PM
#
Your argument doesn't belong to technical aspects of computer science. Unfortunately, you're mixing up the business aspects of software with technical aspects. Optimization and performance evaluation have a solid definition and principles that don't care about financial aspects at all.
I addressed your point in an earlier comment. You may be partially right about the financial aspects but it doesn't have anything to do with this topic and this post. As I had mentioned in the post, such stuff are among the common excuses for sloppiness that I've heard from many developers.
Behzad
Jan 10, 2012 2:33 PM
#
Remember we used Turbo Pascal and C in our universities, these languages has a built-in method to run Assembly codes, I remember asm {} statements in C. Well, let's write the entire program in C !!
Optimization at this level is useless. If you're really interested in Optimization, use Profiling tools to detect bottlenecks in your app.
Keyvan Nayyeri
Jan 10, 2012 4:08 PM
#
If you say so!
Leave a Comment