Questions tagged [premature-optimization]

Premature optimization is the optimizing of code for performance reasons before the code has been measured or profiled to determine if the optimization will actually be beneficial.

Premature optimization is a term coined by Donald Knuth. In general terms, it means "the optimizing of code for performance reasons before the code has been measured or profiled to determine if the optimization will actually be beneficial."

The term "premature optimization" comes from a quote from Knuth's paper "Structured Programming with goto Statements, in which he states:

There is no doubt that the grail of efficiency leads to abuse. 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 %. A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only after that code has been identified. It is often a mistake to make a priori judgments about what parts of a program are really critical, since the universal experience of programmers who have been using measurement tools has been that their intuitive guesses fail.

This quote is often shortened to simply

Premature optimization is the root of all evil

and sometimes used (in its abbreviated form) as an excuse not to optimize code at all, or to eschew critical design decisions involving the choice of appropriate data structures and sensible programming practices.

The best practices embodied by Knuth's observation are these:

  1. Measure code performance first, and then
  2. Optimize only the hot spots, those parts of the code that are causing the greatest impact on performance.

Premature optimization negatively affects debugging and maintenance, because optimized code is typically more difficult to read and understand. Premature optimization can actually slow down a program because the optimizations are confounding the optimizations that the compiler already provides.

74 questions
10
votes
3 answers

In C++, which is better i>-1 or i>=0

This might be a silly question to ask, but this kind of optimization is sometimes boost performance of your application. Here I am asking specifically for C++, because the way C++ compile code is a lot different that c# or Java. The question is…
Yogesh
  • 1,565
  • 1
  • 19
  • 46
9
votes
1 answer

Impact of Java streams from GC perspective or handling short-lived objects by the GC

There are some articles available online where they mention some of the cons of using Stream-s over old…
9
votes
5 answers

Shear a numpy array

I'd like to 'shear' a numpy array. I'm not sure I'm using the term 'shear' correctly; by shear, I mean something like: Shift the first column by 0 places Shift the second column by 1 place Shift the third colum by 2 places etc... So this…
Andrew
  • 2,842
  • 5
  • 31
  • 49
7
votes
1 answer

Do table slices take up memory in R?

If I take a slice of a table using, say the column names, does R allocate memory to hold the slice in a new location? Specifically, I have a table with columns depth1 and depth2, among others. I want to add columns which contain the max and min of…
highBandWidth
  • 16,751
  • 20
  • 84
  • 131
7
votes
6 answers

Why would this Lua optimization hack improve performance?

I'm looking over a document that describes various techniques to improve performance of Lua script code, and I'm shocked that such tricks would be required. (Although I'm quoting Lua, I've seen similar hacks in Javascript). Why would this…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
7
votes
5 answers

Premature optimization or am I crazy?

I recently saw a piece of code at comp.lang.c++ moderated returning a reference of a static integer from a function. The code was something like this int& f() { static int x; x++; return x; } int main() { f()+=1; //A f()=f()+1; //B …
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
7
votes
2 answers

Any good literature on join performance vs systematic denormalization?

As a corollary to this question I was wondering if there was good comparative studies I could consult and pass along about the advantages of using the RDMBS do the join optimization vs systematically denormalizing in order to always access a single…
Newtopian
  • 7,543
  • 4
  • 48
  • 71
6
votes
3 answers

C++: Performance impact of if inside loops

I need to iterate over lots of (2D) data and only sometimes handle special cases. For my application speed is the most critical factor. The options that quickly come to (my) mind are: Option A: more readable performance hit due to comparisons…
Seriously
  • 884
  • 1
  • 11
  • 25
6
votes
10 answers

When is optimization premature?

I see this term used a lot but I feel like most people use it out of laziness or ignorance. For instance, I was reading this article: http://blogs.msdn.com/b/ricom/archive/2006/09/07/745085.aspx where he talks about his decisions he makes to…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
5
votes
5 answers

Is optimizing a class for a unit test good practice, or is it premature?

I've seen (and searched for) a lot of questions on StackOverflow about premature optimization - word on the street is, it is the root of all evil. :P I confess that I'm often guilty of this; I don't really optimize for speed at the cost of code…
user677526
5
votes
1 answer

How expensive is the new Gson() constructor in production?

I am creating a new Netty pipeline and I am trying to: avoid premature optimization. write code that is easy to explain to one of my interns. This sort of factory method is certainly easy to explain: public String toJSON() { Gson gson = new…
Bob Cross
  • 22,116
  • 12
  • 58
  • 95
5
votes
5 answers

Python - optimize by not importing at module level?

In a framework such as Django, I'd imagine that if a user lands on a page (running a view function called "some_page"), and you have 8 imports at the top of module that are irrelevant to that view, you're wasting cycles on those imports. My…
orokusaki
  • 55,146
  • 59
  • 179
  • 257
5
votes
8 answers

Why do Java and C# have bitshifts operators?

Is the difference between integer multiply(temporarily forgetting about division) still in favor of shifting and if so how big is the difference? It simply seems such a low level optimization, even if you wanted it the shouldn't the (C#/Java) to…
Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141
5
votes
3 answers

Java Filters Performance Question

I have two questions. The first is do Filters add a lot of overhead to request. We have a filter and it is set to run on the URL pattern /*. This means it also runs on all the image request. I think that this is not good for performance, but my…
5
votes
3 answers

Am I understanding premature optimization correctly?

I've been struggling with an application I'm writing and I think I'm beginning to see that my problem is premature optimization. The perfectionist side of me wants to make everything optimal and perfect the first time through, but I'm finding this…
Ed Mazur
  • 3,042
  • 3
  • 21
  • 21