GhostZCH/hornet

异常性能测试

GhostZCH opened this issue · 1 comments

异常性能测试

经过测试,异常确实会造成较大的性能损失,但是如果异常的发生概率不大,总体影响并不高

bool func(int i)
{
    shared_ptr<int []> p = shared_ptr<int []>(new int[1000]);
    if (i % 1000 == 0) {
        return false;
    }
    return true;
}


int main(int argc, char const *argv[])
{
    int j = 0;
    for (int i = 0; i < 1000 * 1000; i++) {
        if (!func(i)) {
            j++;
        }
    }

    cout << j << endl;

    return 0;
}

class Error {
public:
    Error(const string& msg, const char* file, int line) {
        msg_ = msg;
        file_ = file;
        line_ = line;
    }

    friend ostream& operator << (ostream& out,const Error& err) {
        out << err.file_ << "[" << err.line_ << "]: " << err.msg_;
        return out;
    }

private:
    int line_;
    string msg_;
    const char* file_;
};


void func(int i)
{
    shared_ptr<int []> p = shared_ptr<int []>(new int[1000]);
    if (i % 1000 == 0) {
        throw Error("my exp", NULL, 0);
    }
}


int main(int argc, char const *argv[])
{
    int j = 0;
    for (int i = 0; i < 1000 * 1000; i++) {
        try {
            func(i);
        }catch (Error& err){
            j++;
        }
    }

    cout << j << endl;

    return 0;
}

➜ exeception time ./speed
1000
./speed 0.06s user 0.00s system 99% cpu 0.063 total
➜ exeception time ./speed1
1000
./speed1 0.06s user 0.00s system 99% cpu 0.057 total