为premature优化的一些注意事项注意事项、premature

2023-09-10 22:45:01 作者:静听〆细水长流

看来,那句 premature优化是一天当中的嗡嗡声字。出于某种原因,iPhone程序员尤其似乎认为避免premature优化作为一个积极的目标,而不是简单地避免分心的自然结果。问题是,该术语开始被越来越多的应用,以是完全不适当的情况

例如,我已经看到有越来越多的人说不要担心算法的复杂性,因为这是premature优化(例如:http://stackoverflow.com/questions/2190275/help-sorting-an-nsarray-across-two-properties-with-nssortdescriptor/2191720#2191720).坦率地说,我觉得这只是懒惰,并震惊地纪律计算机科学。

但它发生,我认为也许考虑的复杂性和算法是怎么回事组装循环展开的方式表现,而现在被认为是不必要的其他优化技术。

你怎么看?难道我们在点现在在哪里为O(n ^ n)和O之间的决定(N!)复杂度的算法是无关紧要的?怎么样为O(n)与O(N * N)?

那你认为premature优化?你用什么实际的规则来自觉或不自觉地避免呢?

修改

我知道我的描述是有点一般,但我感兴趣的是具体,实用的规则人使用或最佳做法,以避免pre-成熟的优化,特别在iPhone平台上的

要回答这个需要你先回答这个问题:什么是pre-成熟的优化?。由于这一定义明确变化如此之大,任何有意义的答案,需要创作者定义的术语。这就是为什么我真的不认为这是一个连续的问题。同样,如果人们不同意,我会改变它。

解决方案   

什么是premature优化?

premature优化是优化code(一般性能)之前,你知道它是否值得这样做的过程中。的premature优化的一个例子是优化code你异形它找出性能瓶颈之前。的premature优化的一个更极端的例子是优化你运行你的程序,并确定其运行速度太慢了。

  

难道我们在点现在所在的O之间的决定(N ^ n)和O(​​N!)复杂度的算法是无关紧要的?怎么样为O(n)与O(N * N)?

这取决于n的大小和频率的code将被调用。

成都网站推广公司浅谈 SEO优化注意事项

如果n是始终小于5则渐近性能是无关紧要的。在这种情况下,常数的大小将变得更为重要。一个简单的为O(n * n)的算法能打败一个更为复杂为O(n log n)的算法,对于小的n。或可测量的差异可能是如此之小,也没关系。

我仍然认为,有太多的人认为花时间优化90%的code,做的10%,这并不重要吧。没有人关心,如果一些code需要10毫秒,而不是1毫秒如果code是很少叫。有些时候,只是这样做,工作和运动上是一个不错的选择,即使你知道算法的复杂性不是最佳的简单的东西。

您花很少的优化名为code每一个小时一个小时不到,你可以花功能的加入,人们实际上想要的。

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 http://stackoverflow.com/questions/2190275/help-sorting-an-nsarray-across-two-properties-with-nssortdescriptor/2191720#2191720). 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.