在C#中的表现费用的'试' [英] Performance Cost Of 'try' in C#
问题描述
我知道,异常有性能损失,并且它的通常更有效地尽量避免例外比下降周围的一切大的try / catch - 但什么try块本身呢?什么是仅仅宣布一个try / catch成本,即使它永远不会抛出异常?
I know that exceptions have a performance penalty, and that it's generally more efficient to try and avoid exceptions than to drop a big try/catch around everything -- but what about the try block itself? What's the cost of merely declaring a try/catch, even if it never throws an exception?
推荐答案
试的性能开销是非常小的。异常处理的主要成本越来越堆栈跟踪和其他元数据,这是该公司没有支付,直到你真正要抛出一个异常的成本。
The performance cost of try is very small. The major cost of exception handling is getting the stack trace and other metadata, and that's a cost that's not paid until you actually have to throw an exception.
但是,这将通过语言和实施而有所不同。为什么不写在C#和时间的一个简单的循环就你自己?
But this will vary by language and implementation. Why not write a simple loop in C# and time it yourself?
这篇关于在C#中的表现费用的'试'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!