Is an empty aliasing shared_ptr a good alternative to a no-op deleting shared_ptr?

戏子无情 提交于 2019-12-03 11:50:21

Concerning performance, the following benchmark shows erratic figures:

#include <chrono>
#include <iostream>
#include <limits>
#include <memory>

template <typename... Args>
auto test(Args&&... args) {
    using clock = std::chrono::high_resolution_clock;
    auto best = clock::duration::max();

    for (int outer = 1; outer < 10000; ++outer) {
        auto now = clock::now();

        for (int inner = 1; inner < 20000; ++inner)
            std::shared_ptr<int> sh(std::forward<Args>(args)...);

        auto time = clock::now()-now;
        if (time < best) {
            best = time;
            outer = 1;
        }
    }

    return best.count();
}

int main()
{
    int j;

    std::cout << "With aliasing ctor: " << test(std::shared_ptr<void>(), &j) << '\n'
              << "With empty deleter: " << test(&j, [] (auto) {});
}

Output on my machine with clang++ -march=native -O2:

With aliasing ctor: 11812
With empty deleter: 651502

GCC with identical options gives an even larger ratio, 5921:465794.
And Clang with -stdlib=libc++ yields a whopping 12:613175.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!