不要试图损害性能/ catch块时异常不抛出? [英] Do try/catch blocks hurt performance when exceptions are not thrown?
问题描述
在与我们遇到了一大段code的来了一个尝试{}
块内的微软员工一个code审查。她和一个IT重新presentative建议这会对code性能的影响。事实上,他们建议大部分code应该是try / catch块之外,那只有重要路段应进行检查。微软员工加入并表示即将到来的白皮书警告不要不正确try / catch块。
During a code review with a Microsoft employee we came across a large section of code inside a try{}
block. She and an IT representative suggested this can have effects on performance of the code. In fact, they suggested most of the code should be outside of try/catch blocks, and that only important sections should be checked. The Microsoft employee added and said an upcoming white paper warns against incorrect try/catch blocks.
我环顾四周,发现它<一个href=\"http://msmvps.com/blogs/peterritchie/archive/2007/06/22/performance-implications-of-try-catch-finally.aspx\">can影响优化,但它似乎当一个变量的作用域之间共享才适用。
I've looked around and found it can affect optimizations, but it seems to only apply when a variable is shared between scopes.
我不要求有关code的维护,甚至处理正确的异常(有问题的code需要重新分解,毫无疑问)。我还没有提到使用异常流量控制,这是在大多数情况下,显然是错误的。这些都是重要的问题(有些是更重要的),而不是这里的重点。
I'm not asking about maintainability of the code, or even handling the right exceptions (the code in question needs re-factoring, no doubt). I'm also not referring to using exceptions for flow control, this is clearly wrong in most cases. Those are important issues (some are more important), but not the focus here.
如何try / catch块影响性能的时候例外的不的抛出?
How do try/catch blocks affect performance when exceptions are not thrown?
编辑:我加入悬赏。还有有趣的反应,但我想获得一些更多的投入。
I'm adding a bounty. There are interesting responses, but I would like to get some more input.
推荐答案
检查一下。
static public void Main(string[] args)
{
Stopwatch w = new Stopwatch();
double d = 0;
w.Start();
for (int i = 0; i < 10000000; i++)
{
try
{
d = Math.Sin(1);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
w.Stop();
Console.WriteLine(w.Elapsed);
w.Reset();
w.Start();
for (int i = 0; i < 10000000; i++)
{
d = Math.Sin(1);
}
w.Stop();
Console.WriteLine(w.Elapsed);
}
输出:
00:00:00.4269033 // with try/catch
00:00:00.4260383 // without.
在毫秒:
449
416
新的code:
for (int j = 0; j < 10; j++)
{
Stopwatch w = new Stopwatch();
double d = 0;
w.Start();
for (int i = 0; i < 10000000; i++)
{
try
{
d = Math.Sin(d);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
d = Math.Sin(d);
}
}
w.Stop();
Console.Write(" try/catch/finally: ");
Console.WriteLine(w.ElapsedMilliseconds);
w.Reset();
d = 0;
w.Start();
for (int i = 0; i < 10000000; i++)
{
d = Math.Sin(d);
d = Math.Sin(d);
}
w.Stop();
Console.Write("No try/catch/finally: ");
Console.WriteLine(w.ElapsedMilliseconds);
Console.WriteLine();
}
新的结果:
try/catch/finally: 382
No try/catch/finally: 332
try/catch/finally: 375
No try/catch/finally: 332
try/catch/finally: 376
No try/catch/finally: 333
try/catch/finally: 375
No try/catch/finally: 330
try/catch/finally: 373
No try/catch/finally: 329
try/catch/finally: 373
No try/catch/finally: 330
try/catch/finally: 373
No try/catch/finally: 352
try/catch/finally: 374
No try/catch/finally: 331
try/catch/finally: 380
No try/catch/finally: 329
try/catch/finally: 374
No try/catch/finally: 334
这篇关于不要试图损害性能/ catch块时异常不抛出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!