可能重复: 性能成本中&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问题:(1) (2)的(3)和(4).
Also see these related SO questions: (1) (2) (3) and (4).