Like in silico said its implementation dependent, but in general exceptions are considered slow for any implementation and shouldn't be used in performance intensive code.
But note that handling exceptions should - as the name says - be the exception rather than the rule in your software design. When you have an application which throws so many exceptions per second that it impacts performance and this is still considered normal operation, then you should rather think about doing things differently.
Note: Andrei Alexandrescu seems to question this "quicker". I personally have seen things swing both ways, some programs being faster with exceptions and others being faster with branches, so there indeed seems to be a loss of optimizability in certain conditions.
这重要吗?
我会说没有。编写程序时应该考虑到 可读性,而不是性能(至少不能作为第一个标准)。当预期调用方不能或不希望当场处理故障并将其传递给堆栈时,可以使用异常。额外的好处: 在 C + + 中,11个异常可以使用标准库在线程之间进行封送处理。
Another important perspective is that the impact of 例外情况的使用 on performance is very different from the isolated efficiency of the supporting language features, because, as the report notes,
当考虑异常处理时,它必须与其他方法进行对比
处理错误
例如:
由于不同的编程风格(正确性)导致的维护成本
冗余呼叫站点 if故障检查与集中式 try
缓存问题(例如,缓存中可能适合较短的代码)
The report has a different list of aspects to consider, but anyway the only practical way to obtain hard facts about the execution efficiency is probably to implement the same program using exception and not using exceptions, within a decided cap on development time, and with developers familiar with each way, and then 量度.
什么是避免异常开销的好方法?
Correctness almost always trumps efficiency.
毫无例外,下列情况很容易发生:
一些代码 P 意味着获取一个资源或计算一些信息。
调用代码 C 应该检查成功/失败,但是没有。
在 C 后面的代码中使用不存在的资源或无效信息,造成一般混乱。
主要的问题是点(2) ,其中通常的 返回码方案不强制调用代码 C 进行检查。
确实有两种主要方法强制进行这种检查:
当异常失败时,P 直接引发异常。
Where P returns an object that C has to 视察 before using its main value (otherwise an exception or termination).
第二种方法是 AFAIK,巴顿和纳克曼在他们的书 * 科学与工程 C + + : 高级技术导论与实例中首次描述了这种情况,他们在书中引入了一个名为 Fallow的类,用于 a & ldquo; may & rdquo; function result。Boost 库现在提供了一个类似的类 optional。而且您可以很容易地自己实现一个 Optional类,使用 std::vector作为非 POD 结果的值载体。
对于第一种方法,调用代码 C 别无选择,只能使用异常处理技术。然而,对于第二种方法,调用代码 C 自己可以决定是执行基于 if的检查,还是执行一般的异常处理。因此,第二种方法支持在程序员与执行时间之间进行效率权衡。
各种 C + + 标准对异常性能的影响是什么?
“I want to know is this still true for C++98”
C + + 98是第一个 C + + 标准。对于异常,它引入了异常类的标准层次结构(不幸的是并不完美)。对性能的主要影响是 异常规范(在 C + + 11中删除)的可能性,然而这种可能性从未被主 Windows C + + 编译器完全实现 Visual C + + : Visual C + + 接受 C + + 98异常规范语法,但只是忽略了异常规范。
C + + 03只是 C + + 98的技术勘误。C + + 03中唯一真正新的是 值初始化。这和例外没有关系。
使用 C + + 11标准的一般异常规范被删除,代之以 noexcept关键字。
C + + 11标准还增加了对存储和重新引发异常的支持,这对于在 C 语言回调中传播 C + + 异常非常有用。这种支持有效地约束了当前异常的存储方式。然而,据我所知,这并不会影响性能,除非在较新的代码中,异常处理可以更容易地用于 C 语言回调的两端。
基本上,这说明使用像 Alexandrescu 描述的异常(50倍减速,因为它们使用 catch作为 else)是错误的。
That being said for ppl who like to do it like that
我希望 C + + 22:)能添加这样的内容:
(注意,这必须是核心语言,因为它基本上是编译器从现有的代码生成)
result = attempt<lexical_cast<int>>("12345"); //lexical_cast is boost function, 'attempt'
//... is the language construct that pretty much generates function from lexical_cast, generated function is the same as the original one except that fact that throws are replaced by return(and exception type that was in place of the return is placed in a result, but NO exception is thrown)...
//... By default std::exception is replaced, ofc precise configuration is possible
if (result)
{
int x = result.get(); // or result.result;
}
else
{
// even possible to see what is the exception that would have happened in original function
switch (result.exception_type())
//...
}