为什么未到达的 try-catch 块会增加运行时间? [英] Why unreached try-catch block increase Runtime time?

查看:28
本文介绍了为什么未到达的 try-catch 块会增加运行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在创建自己的容器 Lib,但我看到无法访问(if 语句无效)try-catch 块增加了运行时间.所以这是我的测试,

I'm currently creating my own container Lib, but I've seen that unreachable(invalidated if statement) try-catch block increased runtime time. So here is my test,

vector.cpp :

template<class Type, class Allocator >
void vector<Type, Allocator >::push_back(Type&& ObjectToPushBack)
{
    if (_capacity == _size)
    {
#if 1
        try
        {
            emplace_back(std::move(ObjectToPushBack));
        }
        catch (NullException& n)
        {
            std::cout << n.what() << std::endl;
            throw n;
        }
#endif
    }
    else
        emplace_back_no_except(std::move(ObjectToPushBack));
}  

Main.cpp :

int _cdecl main()
{
    ctn::vector<TYPE> myvec;

    Timer t;

    myvec.reserve(NB);

    auto f = [&]() {for (int i = 0; i < NB; ++i)myvec.push_back(TYPE());};

    t.timeThisFunction(f, ("My Vector Push Back " + std::to_string(NB) + " 
    Elements").c_str());
}

NB 为 10000000,Type 为 int.

NB is 10000000 and Type is int.

reserve 函数就像 std 中的一样.

reserve function act like the in the std.

Timer 是我创建的一个小库,用于轻松测量时间,它使 std::chrono 过载.

Timer is a little lib that I've created to measure time easily , it overload std::chrono.

try-catch 块的平均时间约为 70 毫秒,注释块的平均时间约为 18 毫秒,这两者之间的差距很大.

The average time with the try-catch block is ~70ms and with the block commented, ~18ms, this is a big gap between the two.

所以,我想知道为什么这个 try-catch 块会增加未被到达的时间(_capacity 等于 _size 仅在最后推送),编译器(MSVC 2017)是否在堆栈上预先分配了 try-catch 块,即使未使用?

So, I want to know why this try-catch block increase the time without being reached( the _capacity equal the _size only after the final push), Is the compilator(MSVC 2017) pre-allocate try-catch block on the stack, even if unused ?

注意:如果您想要 Visual Studio 2017 解决方案,我可以将其发送给您.

NB : if you want the Visual Studio 2017 Solution, i can send it to you.

推荐答案

当您添加 try/catch 块时,编译器会添加代码以支持异常.这是在函数头中执行的(以及为局部变量分配空间和保存寄存器的代码).使用 MSVC,执行的一些异常支持似乎包括设置全局变量以指向本地异常数据、保存此指针的先前值、初始化本地变量以指示函数中的哪个 try/catch 块激活,并设置另一个指向异常处理程序表的局部变量.

When you add in a try/catch block, the compiler adds in code to support exceptions. This is executed in the function header (along with the code to allocate space for local variables and save registers). With MSVC, some of the exception support that is executed appears to consist of setting a global variable to point to the local exception data, saving the previous value of this pointer, initializing a local variable to indicate which try/catch block in the function is active, and setting up another local variable that points to a table of exception handlers.

每当进入或退出 try 块时,活动索引都会更新.

The active index is updated whenever a try block is entered or exited.

其他编译器可以有不同的方式来处理异常.

Other compilers can have different ways to handle exceptions.

这篇关于为什么未到达的 try-catch 块会增加运行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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