问题描述
看来" 过早优化"一词是当天的嗡嗡声.由于某种原因,尤其是iPhone程序员似乎认为避免过早优化是一个积极的目标,而不是简单地避免分心的自然结果.问题是,该术语开始越来越多地应用于完全不合适的案例.
例如,我已经看到越来越多的人说不必担心算法的复杂性,因为这是过早的优化(例如-sorting-an an-nsarray-across-two-properties-with-nssortDescriptor/2191720#2191720">帮助对两个属性(使用NSSortDescriptor?)进行分类.坦白说,我认为这只是懒惰,对纪律严明的计算机科学感到震惊.
,但我已经想到,考虑到算法的复杂性和性能正在采用组装循环展开的方式,而其他现在被认为是不必要的优化技术.
您怎么看?我们现在是否在决定O(n^n)和O(n!)复杂性算法之间是无关紧要的? o(n)vs o(n*n)呢?
您认为"过早优化"?您使用哪些实际规则有意识或无意识地避免使用?
编辑
我知道我的描述有点笼统,但是我对特定的,实用的规则或人们用来避免"预成成优化"的最佳实践,尤其是在iPhone上平台.
回答此问题需要您首先回答"什么是成熟优化?"的问题.由于该定义明显变化很大,因此任何有意义的答案都要求作者定义术语.这就是为什么我真的不认为这是一个CW问题.同样,如果人们不同意,我会更改它.
推荐答案
什么是过早优化?
过早优化是在知道是否值得这样做之前优化代码(通常用于性能)的过程.过早优化的一个示例是在对代码进行介绍之前优化代码以找出性能瓶颈在哪里.在运行程序并确定它运行太慢之前,它的过早优化的一个更极端的示例是优化.
.我们现在在决定O(n^n)和O(n!)复杂性算法之间的地步是无关紧要的吗? o(n)vs o(n*n)呢?
这取决于n的大小和代码的频率.
如果n总是小于5,则渐近性能是无关紧要的.在这种情况下,常数的大小将更重要.一种简单的O(n * n)算法可以击败小n的更复杂的O(n log n)算法.否则可测量的差异可能很小,以至于没关系.
我仍然认为,有太多的人花时间优化90%的代码,而不是10%的代码.如果几乎没有调用该代码,则没有人关心某些代码是否需要10ms而不是1MS.有时候,即使您知道算法复杂性并不是最佳的,只是做一些简单的事情是一个不错的选择.
.每小时您花费的优化很少称为代码,您可以花在添加人们真正想要的功能上的一个小时.
其他推荐答案
我的投票获得大多数人优化他们认为是弱点的东西,但他们没有概述.
因此,无论您多么了解算法,无论您编写的 代码如何,您都不知道模块外发生了什么.您所谓的API在幕后做什么?您能始终认为操作的特定顺序是最快的吗?
这是过早优化的含义.您认为的任何一种优化的事物,尚未通过探索器或其他确定的工具进行严格测试(OPS时钟循环并不是一件坏事,但它仅告诉您性能特征〜实际呼叫比时间更重要,通常比时间更重要,通常),是一个过早的优化.
@k_b说的是我上方的,这也是我所说的.使其正确,使其简单,然后进行轮廓,然后调整.根据需要重复.
其他推荐答案
优先顺序:1.它必须工作 2.必须可维护 3.必须是机器效率
是我第一周的第一周编程课程.在 1982 中.
"过早优化"是在优先级1或2之前考虑的任何时间优先级.
.请注意,现代编程技术(抽象和界面)旨在使此优先级更容易.
一个灰色区域:在最初的设计期间,您 do 必须检查您的解决方案本质上并不是固有的慢速速度.否则,在您至少有一些工作代码之前,不要担心性能.
问题描述
It seems that the phrase "Premature Optimization" is the buzz-word of the day. For some reason, iphone programmers in particular seem to think of avoiding premature optimization as a pro-active goal, rather than the natural result of simply avoiding distraction. The problem is, the term is beginning to be applied more and more to cases that are completely inappropriate.
For example, I've seen a growing number of people say not to worry about the complexity of an algorithm, because that's premature optimization (eg Help sorting an NSArray across two properties (with NSSortDescriptor?)). Frankly, I think this is just laziness, and appalling to disciplined computer science.
But it has occurred to me that maybe considering the complexity and performance of algorithms is going the way of assembly loop unrolling, and other optimization techniques that are now considered unnecessary.
What do you think? Are we at the point now where deciding between an O(n^n) and O(n!) complexity algorithm is irrelevant? What about O(n) vs O(n*n)?
What do you consider "premature optimization"? What practical rules do you use to consciously or unconsciously avoid it?
EDIT
I know my description is a bit general, but I'm interested in specific, practical rules or best practices people use to avoid "pre-mature optimization", particularly on the iphone platform.
Answering this requires you to first answer the question of "what is pre-mature optimization?". Since that definition clearly varies so greatly, any meaningful answer requires the author to define the term. That's why I don't really think this is a CW question. Again, if people disagree, I'll change it.
推荐答案
What is premature optimization?
Premature optimization is the process of optimizing your code (usually for performance) before you know whether or not it is worthwhile to do so. An example of premature optimization is optimizing the code before you have profiled it to find out where the performance bottleneck is. An even more extreme example of premature optimization is optimizing before you have run your program and established that it is running too slowly.
Are we at the point now where deciding between an O(n^n) and O(n!) complexity algorithm is irrelevant? What about O(n) vs O(n*n)?
It depends on the size of n and how often your code will get called.
If n is always less than 5 then the asymptotic performance is irrelevant. In this case the size of the constants will matter more. A simple O(n * n) algorithm could beat a more complicated O(n log n) algorithm for small n. Or the measurable difference could be so small that it doesn't matter.
I still think that there are too many people that spend time optimizing the 90% of code that doesn't matter instead of the 10% that does. No-one cares if some code takes 10ms instead of 1ms if that code is hardly ever called. There are times when just doing something simple that works and moving on is a good choice, even though you know that the algorithmic complexity is not optimal.
Every hour you spend optimizing rarely called code is one hour less that you can spend on adding features people actually want.
其他推荐答案
My vote goes for most people optimize what they think is the weak point, but they don't profile.
So regardless of how well you know algorithms and regardless of how well you've written your code, you don't know what else is happening outside your module. What do the APIs you've called do behind the scenes? Can you always gaurantee that the particular order of ops is the fastest?
This is what is meant by Premature Optimization. Anything that you think is an optimization that has not been rigorously tested by way of a profiler or other definitive tool (clock cycles by ops is not a bad thing, but it only tells you performance characteristics ~ actual calls is more important than timing, usually), is a premature optimization.
@k_b says it well above me, and it's what I say too. Make it right, make it simple, then profile, then tweak. Repeat as necessary.
其他推荐答案
Order of priority: 1. It has to work 2. It has to be maintainable 3. It has to be machine-efficient
That was from the first week of my first programming course. In 1982.
"Premature optimization" is any time Priority 3 has been considered before Priority 1 or 2.
Note that modern programming techniques (abstractions and interfaces) are designed to make this prioritization easier.
The one gray area: during the initial design, you do have to check that your solutions are not inherently cripplingly slow. Otherwise, don't worry about performance until you at least have some working code.