如果是优化premature? [英] When is optimization premature?
问题描述
我看到这个词用了很多,但我觉得大多数人使用它出于懒惰和无知的。举例来说,我正在读这篇文章:
I see this term used a lot but I feel like most people use it out of laziness or ignorance. For instance, I was reading this article:
http://blogs.msdn.com/ B /瑞康/存档/ 2006/09/07 / 745085.aspx
在那里,他谈到他的决定,他向实施必要的类型为他的应用程序。
where he talks about his decisions he makes to implement the types necessary for his app.
如果是我,说起这些为code,我们需要写,其他程序员会想无论是:
If it was me, talking about these for code that we need to write, other programmers would think either:
- 我想太多未来的时候没有什么,因此prematurely优化。
- 过思考不起眼的细节时,有没有速度变慢或性能问题经历。
或两者
和建议,只是实现它,而不用担心这些,直到他们成为一个问题。
and would suggest to just implement it and not worry about these until they become a problem.
这是更preferential?
Which is more preferential?
如何使premature优化之间的区别VS作出知情决定了性能关键应用程序的任何实施之前完成?
How to make the differentiation between premature optimization vs informed decision making for a performance critical application before any implementation is done?
推荐答案
优化是premature如果:
Optimization is premature if:
-
您的应用程序没有做任何事情的时间是至关重要的。 (这意味着,如果你正在写一个程序,增加了500号文件中的,将优化应该不会,甚至流行到你的大脑,因为所有它会做是浪费你的时间。)
Your application isn't doing anything time-critical. (Which means, if you're writing a program that adds up 500 numbers in a file, the word "optimization" shouldn't even pop into your brain, since all it'll do is waste your time.)
您正在做一些对时间要求严格的比装配其他的东西,而且还担心是否我++;我++;
是快或 I + = 2
...如果它的真正的关键,你会工作在组装,而不是浪费时间去担心这一点。 (即使这样,这个特殊的例子最有可能会有问题。)
You're doing something time-critical in something other than assembly, and still worrying whether i++; i++;
is faster or i += 2
... if it's really that critical, you'd be working in assembly and not wasting time worrying about this. (Even then, this particular example most likely won't matter.)
您有一个的的预感的一件东西可能比其他的快一点,但你需要看看它。例如,如果事情是窃听你是否秒表
快或 Environment.TickCount
,这是premature优化,因为如果差异较大,你可能会更肯定,并就不需要来关注一下吧。
You have a hunch that one thing might be a bit faster than the other, but you need to look it up. For example, if something is bugging you about whether StopWatch
is faster or Environment.TickCount
, it's premature optimization, since if the difference was bigger, you'd probably be more sure and wouldn't need to look it up.
如果你有一个猜测的东西可能是缓慢的,但你也不太清楚,只是把 //注意:性能
评论,如果您以后运行?进入瓶颈,检查这些地方在code。我个人并不担心优化,是不是太明显;后来我才使用一个分析器,如果我需要。
If you have a guess that something might be slow but you're not too sure, just put a //NOTE: Performance?
comment, and if you later run into bottlenecks, check such places in your code. I personally don't worry about optimizations that aren't too obvious; I just use a profiler later, if I need to.
另一种方法:
我只是运行我的程序,随意闯入它与调试器,看看它停止了 - 无论它停止可能是一个瓶颈,而更多的时候,它停在那里,瓶颈就越差。它的工作原理几乎像变魔术一样。 :)
I just run my program, randomly break into it with the debugger, and see where it stopped -- wherever it stops is likely a bottleneck, and the more often it stops there, the worse the bottleneck. It works almost like magic. :)
这篇关于如果是优化premature?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!