.NET异常有多慢? [英] How slow are .NET exceptions?

查看:150
本文介绍了.NET异常有多慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想讨论什么时候和不抛出异常。我想解决一个简单的问题。 99%的时间没有抛出异常的争论就是围绕着它们慢,而另一方声称(以基准测试)速度不是问题。我已经阅读了许多博客,文章和关于一方或另一方的文章。那么是什么?

I don't want a discussion about when to and not to throw exceptions. I wish to resolve a simple issue. 99% of the time the argument for not throwing exceptions revolves around them being slow while the other side claims (with benchmark test) that the speed is not the issue. I've read numerous blogs, articles, and posts pertaining one side or the other. So which is it?

答案中的一些链接: Skeet Mariani Brumme

Some links from the answers: Skeet, Mariani, Brumme.

推荐答案

我处于不慢的一面,或者更确切地说,不够慢,使其值得避免在正常使用中。我已经写了两个 short 文章。对于基准测试方面的批评,大多数情况下,在现实生活中会有更多的堆栈要通过,所以你会打击缓存等等 - 但是使用错误代码来工作, em>也打击缓存,所以我看不到这是一个很好的论据。

I'm on the "not slow" side - or more precisely "not slow enough to make it worth avoiding them in normal use". I've written two short articles about this. There are criticisms of the benchmark aspect, which are mostly down to "in real life there'd be more stack to go through, so you'd blow the cache etc" - but using error codes to work your way up the stack would also blow the cache, so I don't see that as a particularly good argument.

只是为了表明 - 我不支持使用异常,它们不合逻辑。例如, int.TryParse 完全适用于从用户转换数据。阅读机器生成的文件是合适的,其中失败的意思是该文件不是它的格式,我真的不想尝试处理这个,因为我不知道还有什么可能是错的。

Just to make it clear - I don't support using exceptions where they're not logical. For instance, int.TryParse is entirely appropriate for converting data from a user. It's appropriate when reading a machine-generated file, where failure means "The file isn't in the format it's meant to be, I really don't want to try to handle this as I don't know what else might be wrong."

在只有合理的情况下使用异常时,我从未看到一个应用程序的性能受到例外的严重影响。基本上,除非你有明显的正确性问题,否则异常不应该经常发生,如果你有明显的正确性问题,那么性能不是你面临的最大的问题。

When using exceptions in "only reasonable circumstances" I've never seen an application whose performance was significantly impaired by exceptions. Basically, exceptions shouldn't happen often unless you've got significant correctness issues, and if you've got significant correctness issues then performance isn't the biggest problem you face.

这篇关于.NET异常有多慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆