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
5
votes
9 answers

Is it premature optimization to develop on slow machines?

We should develop on slow boxen because it forces us to optimize early. Randall Hyde points out in The Fallacy of Premature Optimization, there are plenty of misconceptions around the Hoare quote: We should forget about small efficiencies, say…
Ewan Todd
  • 7,315
  • 26
  • 33
5
votes
3 answers

Is it worthwhile to check the visibility of a DOM element before toggling its visibility? Or is this premature optimization?

Please consider the following jQuery code: if ($(this).is(':hidden')) { $(this).show(); } My Question: Is it worthwhile to check for the element's visibility before issuing the show() command? i.e. Are DOM writes more expensive than DOM…
Jim G.
  • 15,141
  • 22
  • 103
  • 166
4
votes
4 answers

Which piece of code is more performant?

I have some code i'm revewing, which is used to convert some text into an MD5 Hash. Works great. It's used to create an MD5Hhash for a gravatar avatar. Here it is :- static MD5CryptoServiceProvider md5CryptoServiceProvider = null; public static…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
4
votes
4 answers

Creating Local variables in .Net

I just want to know that creating local variables to accept the return value of function is going to hit memory usage or performance in .Net applications , especially in ASP.Net. say MyObject myObject = Foo(); MyOtherObject myOtherObject =…
123Developer
  • 1,463
  • 3
  • 17
  • 24
4
votes
6 answers

Optimization of importing modules in Python

I am reading David Beazley's Python Reference book and he makes a point: For example, if you were performing a lot of square root operations, it is faster to use 'from math import sqrt' and 'sqrt(x)' rather than typing …
user225312
  • 126,773
  • 69
  • 172
  • 181
4
votes
3 answers

When is it a good idea to intern strings manually in a .Net code?

The reference is here: http://msdn.microsoft.com/en-us/library/system.string.intern.aspx Looks like this is done automatically by the compiler a lot, but can also be done manually. Please correct me if I am wrong and shed some more light on…
Hamish Grubijan
  • 10,562
  • 23
  • 99
  • 147
4
votes
3 answers

Java bytecode compiler benchmarks

Q.1. What free compiler produces the most optimal Java bytecode? Q.2. What free virtual machine executes Java bytecode the fastest (on 64-bit multi-core CPUs)? Q.3. What other (currently active) compiler projects are missing from this…
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
4
votes
5 answers

Calling a method n times: should I use a converted for-each loop or a traditional for loop?

Given the need to loop up to an arbitrary int value, is it better programming practice to convert the value into an array and for-each the array, or just use a traditional for loop? FYI, I am calculating the number of 5 and 6 results ("hits") in…
Arvanem
  • 1,043
  • 12
  • 22
3
votes
12 answers

How to write more efficient code

Question of the century? I basically want to know which would be more efficient if I wrote this code as several different variables or if I used small arrays. int x = 34; int y = 28; int z = 293; vs double coordinate[3] = {34, 28, 293}; I have a…
DemiSheep
  • 698
  • 2
  • 15
  • 44
3
votes
6 answers

How to test what method implementation runs faster

While the question check if input is type of string has been closed two of the answers spiked a micro-optimization question in my mind: which of the below two solutions would perform better? Reed Copsey provided a solution using…
ahsteele
  • 26,243
  • 28
  • 134
  • 248
3
votes
2 answers

In Python, is there a way to call a method on every item of an iterable?

Possible Duplicate: Is there a map without result in python? I often come to a situation in my programs when I want to quickly/efficiently call an in-place method on each of the items contained by an iterable. (Quickly meaning the overhead of a…
Thane Brimhall
  • 9,256
  • 7
  • 36
  • 50
3
votes
2 answers

Is there a drawback to having many objective-c categories?

We have some code in objective-c categories which we want to share between projects. There are (at least) two approaches we can take: Put them in one category per class, called something like UIView+SGBExtensions Put then in a number of different…
Simon
  • 25,468
  • 44
  • 152
  • 266
2
votes
1 answer

Does wrapping lock into scope change when lock will be released?

In code fn do_something_under_lock(some_bool_mutex: &Mutex) { do_something_before_lock(); let some_bool = some_bool_mutex.lock().unwrap(); do_something_with_some_bool(some_bool); do_something_after_lock(); } will lock be…
Ivan Ivanyuk
  • 318
  • 2
  • 11
2
votes
1 answer

Should I minimize the usage of db_session in Pony ORM? What is the purpose of db_session?

I am wondering, whether I should minimize usage of db_session or not? Let's consider these two equivalent examples: A) def do_stuff(): with db_session: task = orm.make_proxy(Task.select().first()) task.mark_started() ... …
2
votes
2 answers

Efficiently taking the absolute value of an integer vector in C

The task is to set each element of a C integer array to its absolute value. I'm trying to do it as efficiently as possible. Below are a progression of optimizations that I've made. Please tell me if these are actually optimizations at all, and if…
oink
  • 1,443
  • 2
  • 14
  • 23