C ++异常会导致性能下降? [英] C++ Exceptions Cause Performance Hit?

查看:116
本文介绍了C ++异常会导致性能下降?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我想在我的代码中使用异常来处理错误情况。

我认为这样做很有用,因为它有助于分离go。来自

错误路径的路径。但是,已经提出了一个编码指南,说明

使用传统的错误处理技术而不是例外处理
处理直接的本地错误处理,其中程序

很容易处理自己的错误。


通过传统的错误处理,我相信他们的意思是返回一个

错误代码,或者只是处理错误而不会进入catch块。

当我说我更喜欢抛出和捕获异常时 - 事实上,实际上,例外是传统的错误处理技术。在

C ++中,有人告诉我,既然我们有一个实时系统,我们买不起

因使用异常导致的性能损失。


异常块会导致大的性能命中吗?如果是这样,是什么导致

命中?或者这个人被误导了?


感谢您提供任何信息,


Ken

[见 http://www.gotw.ca/resources/clcm.htm 获取信息关于]

[comp.lang.c ++。主持。第一次海报:做到这一点! ]

Hi. I wanted to use exceptions to handle error conditions in my code.
I think doing that is useful, as it helps to separate "go" paths from
error paths. However, a coding guideline has been presented that says
"Use conventional error-handling techniques rather than exception
handling for straightforward local error processing in which a program
is easily able to deal with its own errors."

By "conventional error-handling," I believe they mean returning an
error code, or just handling the error without going to a catch block.
When I said that I''d prefer throwing and catching exceptions--and that,
in fact, exceptions is the "conventional error-handling technique" in
C++, I was told that since we have a real-time system, we can''t afford
the performance hit caused by using exceptions.

Do exception blocks cause big performance hits? If so, what causes the
hit? Or is the person just misinformed?

Thanks for any info,

Ken
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

kk **** @ yahoo.com 写道:
嗨。我想在我的代码中使用异常来处理错误条件。
我认为这样做很有用,因为它有助于分离go。来自错误路径的路径。但是,已经提出了一个编码指南,说明使用传统的错误处理技术而不是异常处理直接的本地错误处理,其中程序很容易处理它的问题。自己的错误。


这里的关键词是* local *。抛出一个例外基本上是一个非本地的'去''进行相应的簿记 - 需要付出相当费用

来设置簿记。
通过传统的错误处理,我相信他们的意思是返回一个
错误代码,或者只是处理错误而不去陷阱。

这是正确的。
当我说我更喜欢抛出和捕获异常 - 实际上,异常是传统的错误处理技术。在C ++中,我被告知,由于我们有一个实时系统,我们无法承受因使用异常而导致的性能损失。

异常阻塞导致大的表现命中?如果是这样,是什么导致了什么?或者是被误导的人?
Hi. I wanted to use exceptions to handle error conditions in my code.
I think doing that is useful, as it helps to separate "go" paths from
error paths. However, a coding guideline has been presented that says
"Use conventional error-handling techniques rather than exception
handling for straightforward local error processing in which a program
is easily able to deal with its own errors."
The key word here is *local*. Throwing an exception is essentially a
non-local `go to'' that does the appropriate bookkeeping -- at the cost
of setting up that bookkeeping.
By "conventional error-handling," I believe they mean returning an
error code, or just handling the error without going to a catch block.
That is correct.
When I said that I''d prefer throwing and catching exceptions--and that,
in fact, exceptions is the "conventional error-handling technique" in
C++, I was told that since we have a real-time system, we can''t afford
the performance hit caused by using exceptions.

Do exception blocks cause big performance hits? If so, what causes the
hit? Or is the person just misinformed?



成本是(编制者)设置簿记信息

进行非本地跳转(按顺序)为所有本地创建的对象调用所有必要的

析构函数)。当一个错误可以在本地处理时(即没有必要明确地传播错误,因为
深度调用堆栈展开),这是值得的。另一方面,如果

调用堆栈*是*深,使用异常(当然,在

*例外*情况下),同时有成本,使代码

在概念上更加清晰。与其他任何事情一样,这是一种权衡。


HTH,

--ag


[ clcm elided]

-

Artie Gold - 德克萨斯州奥斯汀
http://it-matters.blogspot.com (新帖子12/5)
http://www.cafepress.com/goldsays


The cost is (the compiler) setting up the bookkeeping information
necessary to make the non-local jump (in order call all necessary
destructors for any locally created objects). When an error can be dealt
with locally (i.e. there''s no need to explicitly propagate the error as
deep call stack unwinds) it is worth doing so. If, on the other hand,
the call stack *is* deep, the use of exceptions (naturally, in
*exceptional* situations), while having a cost, makes the code
conceptually much clearer. As with anything else, it''s a trade off.

HTH,
--ag

[c.l.c.m elided]
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays


kk **** @ yahoo.com 写道:
kk****@yahoo.com wrote:
我想用异常处理我的错误情况代码。
我认为这样做很有用,因为它有助于分离去。错误路径的路径。




但是,已经提出了一个编码指南,说明使用传统的错误处理技术而不是异常处理用于直接的本地错误处理,其中程序可以轻松地处理其自身的错误。


这是模糊的,但可能是合理的建议。

通过传统的错误处理,
我相信他们的意思是返回错误代码
或只是处理错误而不进入catch块。


你最好要求澄清一下。


异常(你称之为错误)应该处理
$ b在可能的情况下首次检测到它们的$ b。

如果在首次检测到它们的函数中它们无法完全处理



你*必须*创建一个异常对象

其中包含所需的所有信息

来处理调用程序中的异常

并返回或抛出异常对象。

在某些情况下,错误代码可能就足够了

来包含所有信息

需要处理异常。

如果没有,你可能需要一个更复杂的异常对象。

当我说我更喜欢抛出和捕获异常时,事实上,例外是传统的错误处理技术。在C ++中,我被告知,由于我们有一个实时系统,
我们无法承受使用异常导致的性能损失。

做异常块会导致大的性能下降吗?
如果是这样,是什么原因引起了打击?
或者这个人只是被误导了?
I want to use exceptions to handle error conditions in my code.
I think doing that is useful,
as it helps to separate "go" paths from error paths.
?
However, a coding guideline has been presented that says
"Use conventional error-handling techniques rather than exception handling
for straightforward local error processing
in which a program is easily able to deal with its own errors."
That''s vague but possibly sound advice.
By "conventional error-handling,"
I believe they mean returning an error code
or just handling the error without going to a catch block.
You had better ask for some clarification on this.

Exceptions (what you call errors) should be handled
at the point where they are first detected if possible.
If they can''t be completely handled
in the function where they are first detected,
you *must* create an exception object
which contains all of the information required
to handle the exception in the calling program
and return or throw the exception object.
In some cases, an error code may be sufficient
to contain all of the information
required to handle the exception.
If not, you may need a more complicated exception object.
When I said that I''d prefer throwing and catching exceptions and that,
in fact, exceptions [are] the "conventional error-handling technique" in C++,
I was told that, since we have a real-time system,
we can''t afford the performance hit caused by using exceptions.

Do exception blocks cause big performance hits?
If so, what causes the hit?
Or is the person just misinformed?




再次,你应该要求澄清。

异常*不*影响性能

除非遇到异常

然后它们可能只是效率很高
作为任何所谓的传统错误处理技术。

实时编程的问题是

你*必须*知道需要多长时间执行你的代码。

如果你的任何异常(错误)处理都在关键路径上,

你必须能够建立一个上限

表示完成所需的时间。

我怀疑你的主管认为他知道如何做到这一点

为传统错误 R-处理"但是没有

的C ++异常处理机制。


无论如何,我用过Google

http://www.google.com/

搜索


+实时编程 +C ++异常处理


我找到了很多东西。



Again, you should ask for clarification.
Exceptions do *not* affect performance
unless an exception is encountered
and then they are probably just about as efficient
as any so-called conventional error-handling technique.
The problem with real-time programming is that
you *must* know how long it takes to execute your code.
If any of your exception (error) handling is on the critical path,
you must be able to establish an upper bound
for the time that it will take to complete.
I suspect that your supervisor thinks (s)he knows how to do that
for "conventional error-handling" but not
for the C++ exception handling mechanism.

Anyway, I used Google

http://www.google.com/

to search for

+"real-time programming" +"C++ exception handling"

and I found lots of stuff.


" E. Robert Tisdale <,E ************** @ jpl.nasa.gov>。在消息中写道

news:db ********** @ntp1.jpl.nasa.gov
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote in message
news:db**********@nntp1.jpl.nasa.gov
例外做*不*影响表现除非遇到异常
Exceptions do *not* affect performance
unless an exception is encountered




这取决于编译器。在VC ++上,当没有抛出异常时,性能成本甚至很低。


-

John Carson



That is compiler dependent. On VC++, there is a small performance cost even
when exceptions are not thrown.

--
John Carson


这篇关于C ++异常会导致性能下降?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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