When is optimisation premature?

前端 未结 20 1200
悲哀的现实
悲哀的现实 2020-11-21 23:26

As Knuth said,

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.

相关标签:
20条回答
  • First, get the code working. Second, verify that the code is correct. Third, make it fast.

    Any code change that is done before stage #3 is definitely premature. I am not entirely sure how to classify design choices made before that (like using well-suited data structures), but I prefer to veer towards using abstractions taht are easy to program with rather than those who are well-performing, until I am at a stage where I can start using profiling and having a correct (though frequently slow) reference implementation to compare results with.

    0 讨论(0)
  • 2020-11-22 00:21

    What you seem to be talking about is optimization like using a hash-based lookup container vs an indexed one like an array when a lot of key lookups will be done. This is not premature optimization, but something you should decide in the design phase.

    The kind of optimization the Knuth rule is about is minimizing the length the most common codepaths, optimizing the code that is run most by for example rewriting in assembly or simplifying the code, making it less general. But doing this has no use until you are certain which parts of code need this kind of optimization and optimizing will (could?) make the code harder to understand or maintain, hence "premature optimization is the root of all evil".

    Knuth also says it is always better to, instead of optimizing, change the algorithms your program uses, the approach it takes to a problem. For example whereas a little tweaking might give you a 10% increase of speed with optimization, changing fundamentally the way your program works might make it 10x faster.

    In reaction to a lot of the other comments posted on this question: algorithm selection != optimization

    0 讨论(0)
  • 2020-11-22 00:21

    I try to only optimise when a performance issue is confirmed.

    My definition of premature optimisation is 'effort wasted on code that is not known to be a performance problem.' There is most definitely a time and place for optimisation. However, the trick is to spend the extra cost only where it counts to the performance of the application and where the additional cost outweighs the performance hit.

    When writing code (or a DB query) I strive to write 'efficient' code (i.e. code that performs its intended function, quickly and completely with simplest logic reasonable.) Note that 'efficient' code is not necessarily the same as 'optimised' code. Optimisations often introduce additional complexity into code which increases both the development and maintenance cost of that code.

    My advice: Try to only pay the cost of optimisation when you can quantify the benefit.

    0 讨论(0)
  • 2020-11-22 00:21

    The way I see it is, if you optimize something without knowing how much performance you can gain in different scenario IS a premature optimization. The goal of code should really making it easiest for human to read.

    0 讨论(0)
  • 2020-11-22 00:22

    I don't think that recognized best practices are premature optimizations. It's more about burning time on the what ifs that are potential performance problems depending on the usage scenarios. A good example: If you burn a week trying to optimize reflecting over an object before you have proof that it is a bottleneck you are prematurely optimizing.

    0 讨论(0)
  • 2020-11-22 00:23

    The need to use a profiler should be left for extreme cases. The engineers of the project should be aware of where performance bottlenecks are.

    I think "premature optimisation" is incredibly subjective.

    If I am writing some code and I know that I should be using a Hashtable then I will do that. I won't implement it in some flawed way and then wait for the bug report to arrive a month or a year later when somebody is having a problem with it.

    Redesign is more costly than optimising a design in obvious ways from the start.

    Obviously some small things will be missed the first time around but these are rarely key design decisions.

    Therefore: NOT optimising a design is IMO a code smell in and of itself.

    0 讨论(0)
提交回复
热议问题