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

查看:21
本文介绍了.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?

答案中的一些链接:SkeetMariani, 布鲁姆.

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

推荐答案

我站在不慢"的一边——或者更准确地说不够慢,值得在正常使用中避免它们".我写了两个 short 文章 关于这个.有对基准方面的批评,主要归结为在现实生活中会有更多的堆栈要通过,所以你会炸毁缓存等"——但是使用错误代码在堆栈中向上工作会破坏缓存,所以我不认为这是一个特别好的论点.

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天全站免登陆