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
0
votes
3 answers

What is a good size for medium sized memory allocations?

For a serializing system, I need to allocate buffers to write data into. The size needed is not known in advance, so the basic pattern is to malloc N bytes and use realloc if more is needed. The size of N would be large enough to accommodate most…
porgarmingduod
  • 7,668
  • 10
  • 50
  • 83
0
votes
8 answers

which bit-manipulation method is more efficient in C?

Base on the answers i've got, i think this problem is kind of meaningless. Thanks for all your kind replies! i want to get a binary number with its rightmost j bits set to 1 and others set to be 0. basically, there are two methods. i wanna know…
Lion
  • 965
  • 10
  • 21
0
votes
2 answers

Possible redundant object creation with Optional#ofNullable?

I'm curious about a usage of Optional. With following code snippet, public List read( @QueryParam("first_result") @Min(0) final Integer firstResult, @QueryParam("max_results") @Min(0) final Integer maxResults) { // ... …
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
0
votes
3 answers

Calling a method on an object a bunch of times versus constructing an object a bunch of times

I have a List called myData and I want to apply a particular method (someFunction) to every element in the List. Is calling a method through an object's constructor slower than calling the same method many times for one particular object…
Amichai
  • 1,144
  • 1
  • 12
  • 21
0
votes
2 answers

Should i use HttpResponse.End() for a fast webapp?

HttpResponse.End() seems to throw an exception according to msdn. Right now i have the choice of returning a value to say end thread (it only goes 2 functions deep) or i can call end(). I know that throwing exceptions is significantly slower (read…
user34537
0
votes
1 answer

Fragmentation in SQLite used in a round-robin fashion without VACUUM

There's an SQLite database being used to store static-sized data in a round-robin fashion. For example, 100 days of data are stored. On day 101, day 1 is deleted and then day 101 is inserted. The number of rows is the same between days. The the…
Calpau
  • 921
  • 10
  • 21
0
votes
1 answer

Worse framerate with Object Pool

I am working on a game. And I have now read a couple of articles suggesting I should be re-using objects instead of creating new ones to reduce the frame drop when Garbage Collection sweeps in and removes objects. Articles read on the…
0
votes
2 answers

Getting value from object vs getting value from global value

I know this is premature optimization but I am just curious to know how long does it take to get the value of this var objects = { number:10 } console.log(""+objects.number); VS var number = 10; console.log("" + number); I just do not know how…
user3213315
0
votes
2 answers

Which one of this syntaxs have better performance?

Which one of this syntax have better performance and speed in searching between data? First alternative: this.Message = pageContentsli .Where(m => m.PName == "Message") .First() .ContentValue; Second alternative: foreach…
motevalizadeh
  • 5,244
  • 14
  • 61
  • 108
0
votes
5 answers

In terms of today's technology, are these meaningful concerns about data size?

We're adding extra login information to an existing database record on the order of 3.85KB per login. There are two concerns about this: 1) Is this too much on-the-wire data added per login? 2) Is this too much extra data we're storing in the…
Alan
  • 45,915
  • 17
  • 113
  • 134
-1
votes
1 answer

All possible Combinations for this set of letternumbers

I am asking for help to figure out all possible combinations for these sets A1orA2,B1orB2,C1orC2,D1orD2,E1orE2 I attached a picture that is how I want it. And you can only have 1 or 2 in each combination, not both. EX - A1 or A2 Thank you Here is…
-1
votes
2 answers

Python | efficiency and performance

Lets say I'm going to save 100 floating point numbers in a list by running a single script, most probably it will take some memory to process.So if this code executes every time as a requirement of an application there will be performance hits, so…
Switch
  • 14,783
  • 21
  • 69
  • 110
-2
votes
2 answers

Remove the delimiter , at the end

String prefix = ""; for (String serverId : serverIds) { sb.append(prefix); prefix = ","; sb.append(serverId); } The following code runs faster than the above code . the "," prefix object does unnecessary object creation on every iteration .…
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112
-3
votes
2 answers

How to optimize a statement of the form "if (A == B) { ...} else if (A < B) {...} else { ....}"

I have a piece of code that's like if (A == B) { ... } else if (A < B) { ... } else // (A > B) { ... } I realize there is a redundancy problem because there will be some of the same bit comparisons going into the computation of == and…
Donald Knuth
  • 129
  • 4
1 2 3 4
5