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
112
votes
12 answers

Are Java static calls more or less expensive than non-static calls?

Is there any performance benefit one way or another? Is it compiler/VM specific? I am using Hotspot.
Andy Faibishenko
  • 1,171
  • 2
  • 9
  • 4
90
votes
20 answers

When is optimisation premature?

As Knuth said, We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. This is something which often comes up in Stack Overflow answers to questions like "which is the most efficient…
nickf
  • 537,072
  • 198
  • 649
  • 721
88
votes
6 answers

Which "if" construct is faster - statement or ternary operator?

There are two types of if statements in java - classic: if {} else {} and shorthand: exp ? value1 : value2. Is one faster than the other or are they the same? statement: int x; if (expression) { x = 1; } else { x = 2; } ternary operator: int x…
Rogach
  • 26,050
  • 21
  • 93
  • 172
62
votes
8 answers

What is the Cost of Calling array.length

While updating for loops to for-each loops in our application, I came across a lot of these "patterns": for (int i = 0, n = a.length; i < n; i++) { ... } instead of for (int i = 0; i < a.length; i++) { ... } I can see that you gain…
Stroboskop
  • 4,327
  • 5
  • 35
  • 52
41
votes
11 answers

Is ++i really faster than i++ in for-loops in java?

In java I usually make a for-loop like following: for (int i = 0; i < max; i++) { something } But recently a colleague typed it so: for (int i = 0; i < max; ++i) { something } He said the latter would be faster. Is that true?
Binabik
  • 437
  • 1
  • 4
  • 6
39
votes
11 answers

x > -1 vs x >= 0, is there a performance difference

I have heard a teacher drop this once, and it has been bugging me ever since. Let's say we want to check if the integer x is bigger than or equal to 0. There are two ways to check this: if (x > -1){ //do stuff } and if (x >= 0){ //do…
Cheiron
  • 3,620
  • 4
  • 32
  • 63
35
votes
19 answers

Can knowing C actually hurt the code you write in higher level languages?

The question seems settled, beaten to death even. Smart people have said smart things on the subject. To be a really good programmer, you need to know C. Or do you? I was enlightened twice this week. The first one made me realize that my assumptions…
22
votes
6 answers

Java foreach efficiency

I have something like this: Map myMap = ...; for(String key : myMap.keySet()) { System.out.println(key); System.out.println(myMap.get(key)); } So is myMap.keySet() called once in the foreach loop? I think it is, but want…
Flueras Bogdan
  • 9,187
  • 8
  • 32
  • 30
22
votes
2 answers

ColdFusion: More efficient structKeyExists() instead of isDefined()

Which of these is more efficient in ColdFusion? isDefined('url.myvar') or structKeyExists(url, 'myvar')
James T
  • 3,292
  • 8
  • 40
  • 70
22
votes
7 answers

Is count(*) really expensive?

I have a page where I have 4 tabs displaying 4 different reports based off different tables. I obtain the row count of each table using a select count(*) from query and display number of rows available in each table on the tabs. As a…
Anil Namde
  • 6,452
  • 11
  • 63
  • 100
22
votes
3 answers

At which n does binary search become faster than linear search on a modern CPU?

Due to the wonders of branch prediction, a binary search can be slower than a linear search through an array of integers. On a typical desktop processor, how big does that array have to get before it would be better to use a binary search? Assume…
joeforker
  • 40,459
  • 37
  • 151
  • 246
20
votes
13 answers

Calling a getter multiple times or calling once and assigning to a variable?

say suppose I have class as : public class Age { private int age; public int getAge() { return this.age; } } In my Main class I am calling the getAge() method many times. So I wanted to know is it advisable to call so many…
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
13
votes
5 answers

Why swap doesn't use Xor operation in C++

I've learned that Xor operation can be used to implement effective swap function. like this: template void swap(T& a, T& b) { a = a^b; b = a^b; a = a^b; } But the implementation of swap all i can found on the internet is…
user955249
11
votes
13 answers

Does rearranging a conditional evaluation speed up a loop?

Bit of a weird one: I was told a while ago by a friend that rearranging this example for loop from : for(int i = 0; i < constant; ++i) { // code... } to: for(int i = 0; constant > i; ++i) { // code... } would slightly increase performance…
10
votes
6 answers

Java: Performance of Enums vs. if-then-else

I've had no real luck getting a concise answer for this comparison by using Google and rather than do my own time consuming evaluations, I thought I would ask first. I'm fairly sure that a switch statement using Enums would perform faster than an…
FizzBuzz
  • 558
  • 2
  • 13
  • 29
1
2 3 4 5