一个try / catch块的性能成本 [英] Performance cost of a try/catch block
问题描述
可能重复:
性能成本中&lsquo的;尝试&rsquo的;
我被告知,加入try catch块增加主要性能成本的1000倍,比不慢的顺序,在一个例子为一百万的循环。这是真的吗?
I am being told that adding a try catch block adds major performance cost in the order of 1000 times slower than without, in the example of a for loop of a million. Is this true?
这难道不是最好使用try catch块尽可能?
Isn't it best to use try catch block as much as possible?
推荐答案
从 MSDN网站
查找和设计走 异常沉重code能导致 体面PERF获胜。请记住, 这有没有关系的try / catch 块:你只能承担的费用时, 实际的异常。您 可以使用尽可能多的try / catch块, 你想。使用异常 无偿为你失去 性能。例如,你应该 远离像使用 例外的控制流。
Finding and designing away exception-heavy code can result in a decent perf win. Bear in mind that this has nothing to do with try/catch blocks: you only incur the cost when the actual exception is thrown. You can use as many try/catch blocks as you want. Using exceptions gratuitously is where you lose performance. For example, you should stay away from things like using exceptions for control flow.
另请参阅这些相关的SO问题:<一href="http://stackoverflow.com/questions/1308432/do-try-catch-blocks-hurt-performance-when-exceptions-are-not-thrown">(1) (2)的(3)和<一href="http://stackoverflow.com/questions/2075151/try-catch-blocks-always-expensive-closed">(4).
Also see these related SO questions: (1) (2) (3) and (4).
这篇关于一个try / catch块的性能成本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!