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
2
votes
1 answer

SQLite vacuuming / fragmentation and performance degradation

Let's say I periodically insert data into a SQLite database, then purge the first 50% of the data, but I don't vacuum. Do I have something like zeroed-out pages for the first 50% of the file now? If I add another batch of data, am I filling in those…
Calpau
  • 921
  • 10
  • 21
2
votes
6 answers

Additional 'if checks' if the value is already set up - what is faster, what uses more resources?

Assume that we have a given interface: public interface StateKeeper { public abstract void negateWithoutCheck(); public abstract void negateWithCheck(); } and following implementations: class StateKeeperForPrimitives implements…
2
votes
4 answers

Why don't people use xor swaps?

I read on a site that using xor swaps is fast because it doesn't use a temporary variable. Here's an example: #include int main(void) { int a=234,b=789; b=b^a; a=b^a; b=b^a; printf("a=%d,b=%d",a,b); return…
autistic
  • 1
  • 3
  • 35
  • 80
2
votes
2 answers

Is there significant overhead for this particular inner non-static class?

I am writing my own implementation of a doubly linked list for school assignment and I use an inner node class called Node inside the list class, which represents list nodes that are linked to each other (as is usually the case with linked…
Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95
2
votes
4 answers

passing a string by reference to a function would speed things up? (php)

Possible Duplicate: In PHP (>= 5.0), is passing by reference faster? I wonder if by declaring the parameter pass by reference, the PHP interpreter will be faster for not having to copy the string to the function's local scope? The script turns…
Petruza
  • 11,744
  • 25
  • 84
  • 136
1
vote
4 answers

IF Statements: What's more efficient? Many small ones, or a couple large ones?

I have form for my recipe app that inserts ingredients into a db. If nothing has been submitted yet, nutritional values display '0'. Otherwise they update to the post values. My structure for the nutritional display is: Calories:
elev8
  • 25
  • 3
1
vote
1 answer

Is it a good practice to instantiate a HashSet from an IEnumerable before using Contains()?

The piece of code below filters an IEnumerable with another, used as a blacklist. The filtered collection iterates over content fetched remotely (lazy loading, YouTube API). IEnumerable contentThatCanBeHuge =…
Amessihel
  • 5,891
  • 3
  • 16
  • 40
1
vote
1 answer

Is it worth moving `let` out of loop?

In this code fn do_something_under_lock( is_locked: &AtomicBool, ) { loop { let lock_acquired = is_locked .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) .is_ok(); if…
Ivan Psov
  • 11
  • 4
1
vote
2 answers

Can I cache getClass.hashCode()?

Whatever the reason, I have the following hashCode implemented in my abstract class. @MappedSuperclass abstract Some { @Override public boolean equals(final Object obj) { // ... } @Override public int hashCode() { …
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
1
vote
1 answer

Is there an example where using React.memo/PureComponent has worse performance than not using it?

I have seen many times in discussions and articles (many of which are from developers of React) mentioning that React.memo or PureComponent comes with performance overhead, and should be applied only after actually measuring the performance. But, as…
1
vote
2 answers

Narrow scope of a variable and performance?

I have a main method looks like this. struct list *l = array_list(); if (l == NULL) { return EXIT_FAILURE; } srand(time(NULL)); int size = 4; for (int i = 0; i < size; i++) { int *d = malloc(sizeof(int)); …
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
1
vote
3 answers

perl string catenation and substitution in a single line?

I need to modify a perl variable containing a file path; it needs to begin and end with a forward slash (/) and have all instances of multiple forward slashes reduced to a single slash. (This is because an existing process does not enforce a…
Medievalist
  • 713
  • 5
  • 8
1
vote
2 answers

Take(10) vs TOP 10 With SqlDataReader?

I have a method that reads data using SqlDataReader and yield returns an IEnumerable, e.g.: IEnumerable LoadCustomers() { using(SqlDataReader rdr = cmd.ExecuteReader()) { while (rdr.Read()) { yield return rdr.GetString(0);…
BornToCode
  • 9,495
  • 9
  • 66
  • 83
1
vote
4 answers

Computation overhead in C# - Using getters/setters vs. modifying arrays directly and casting speeds

I was going to write a long-winded post, but I'll boil it down here: I'm trying to emulate the graphical old-school style of the NES via XNA. However, my FPS is SLOW, trying to modify 65K pixels per frame. If I just loop through all 65K pixels and…
Jeffrey Kern
  • 2,024
  • 20
  • 40
0
votes
0 answers

Object dynamic constness

I am right now facing issue with my design. I am handling situation of master-server and semi-slave-client. I have server which practically speaking – can do whatever it desires. (Lets don't deconstruct network layer, as it is already working…
Black
  • 312
  • 2
  • 15