引发C ++ 0x异常的代价 [英] Cost of throwing C++0x exceptions

查看:72
本文介绍了引发C ++ 0x异常的代价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 0x中引发异常对性能有何影响?这个编译器有多少依赖?这与询问 try 块的成本是多少。

What are the performance implications of throwing exceptions in C++0x? How much is this compiler dependent? This is not the same as asking what is the cost of entering a try block, even if no exception is thrown.

我们应该期望在Java之类的常规逻辑处理中更多地使用异常吗?

Should we expect to use exceptions more for general logic handling like in Java?

推荐答案

#include <iostream>
#include <stdexcept>

struct SpaceWaster {
    SpaceWaster(int l, SpaceWaster *p) : level(l), prev(p) {}
    // we want the destructor to do something
    ~SpaceWaster() { prev = 0; }
    bool checkLevel() { return level == 0; }
    int level;
    SpaceWaster *prev;
};

void thrower(SpaceWaster *current) {
    if (current->checkLevel()) throw std::logic_error("some error message goes here\n");
    SpaceWaster next(current->level - 1, current);
    // typical exception-using code doesn't need error return values
    thrower(&next);
    return;
}

int returner(SpaceWaster *current) {
    if (current->checkLevel()) return -1;
    SpaceWaster next(current->level - 1, current);
    // typical exception-free code requires that return values be handled
    if (returner(&next) == -1) return -1;
    return 0;
}

int main() {
    const int repeats = 1001;
    int returns = 0;
    SpaceWaster first(1000, 0);

    for (int i = 0; i < repeats; ++i) {
        #ifdef THROW
            try {
                thrower(&first);
            } catch (std::exception &e) {
                ++returns;
            }
        #else
            returner(&first);
            ++returns;
        #endif
    }
    #ifdef THROW
        std::cout << returns << " exceptions\n";
    #else
        std::cout << returns << " returns\n";
    #endif
}

米老鼠基准测试结果:

$ make throw -B && time ./throw
g++     throw.cpp   -o throw
1001 returns

real    0m0.547s
user    0m0.421s
sys     0m0.046s

$ make throw CPPFLAGS=-DTHROW -B && time ./throw
g++  -DTHROW   throw.cpp   -o throw
1001 exceptions

real    0m2.047s
user    0m1.905s
sys     0m0.030s

因此,在这种情况下,将异常抛出1000个堆栈级别,而不是正常返回,大约需要1.5毫秒这包括进入try块,我相信在某些系统上,该代码在执行时是免费的,在其他系统上,每次输入try都会产生费用,而在另一些系统上,每次您输入包含try的函数只会产生费用。对于更可能的100个堆栈级别,我将重复次数提高到10k,因为一切都快了10倍。因此,异常的代价为0.1毫秒。

So in this case, throwing an exception up 1000 stack levels, rather than returning normally, takes about 1.5ms. That includes entering the try block, which I believe on some systems is free at execution time, on others incurs a cost each time you enter try, and on others only incurs a cost each time you enter the function which contains the try. For a more likely 100 stack levels, I upped the repeats to 10k because everything was 10 times faster. So the exception cost 0.1ms.

对于1万个堆栈级别,这是18.7s相对于4.1s,因此,异常的额外成本约为14ms。因此,在此示例中,我们正在研究相当稳定的每层堆栈1.5us的开销(每个级别都在破坏一个对象)。

For 10 000 stack levels, it was 18.7s vs 4.1s, so about 14ms additional cost for the exception. So for this example we're looking at a pretty consistent overhead of 1.5us per level of stack (where each level is destructing one object).

显然C ++ 0x没有为异常(或其他任何东西,除了算法和数据结构的big-O复杂性)指定性能。我认为它不会以某种方式对异常产生积极或负面影响的例外更改。

Obviously C++0x doesn't specify performance for exceptions (or anything else, other than big-O complexity for algorithms and data structures). I don't think it changes exceptions in a way which will seriously impact many implementations, either positively or negatively.

这篇关于引发C ++ 0x异常的代价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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