C# 中 try/catch 的真正开销是多少? [英] What is the real overhead of try/catch in C#?

查看:35
本文介绍了C# 中 try/catch 的真正开销是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我知道 try/catch 确实会增加一些开销,因此不是控制流程的好方法,但是这种开销从何而来,它的实际影响是什么?

So, I know that try/catch does add some overhead and therefore isn't a good way of controlling process flow, but where does this overhead come from and what is it's actual impact?

推荐答案

我不是语言实现方面的专家(所以持保留态度),但我认为最大的成本之一是展开堆栈和存储它用于堆栈跟踪.我怀疑只有在抛出异常时才会发生这种情况(但我不知道),如果是这样,每次抛出异常时,这都会是相当大的隐藏成本......所以这不像你只是从一个地方跳下来在另一个代码中,有很多事情要做.

I'm not an expert in language implementations (so take this with a grain of salt), but I think one of the biggest costs is unwinding the stack and storing it for the stack trace. I suspect this happens only when the exception is thrown (but I don't know), and if so, this would be decently sized hidden cost every time an exception is thrown... so it's not like you are just jumping from one place in the code to another, there is a lot going on.

我不认为这是一个问题,只要您对异常行为使用异常(所以不是您典型的、预期的程序路径).

I don't think it's a problem as long as you are using exceptions for EXCEPTIONAL behavior (so not your typical, expected path through the program).

这篇关于C# 中 try/catch 的真正开销是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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