【问题标题】:Why std::make_shared<>() has much better performance than boost::make_shared()?为什么 std::make_shared<>() 比 boost::make_shared() 有更好的性能?
【发布时间】:2012-10-07 14:16:23
【问题描述】:

我一直在做一些现场性能测试

1>std::shared_ptr, std::make_shared based on 'gcc 4.7.2' & 'VC10 implementation' 
2>boost::shared_ptr, boost::make_shared based on boost 1.47

测试结果有点意思。

1>一般std 版本性能更好,但尤其是std::make_shared。为什么?我可以提高 boost 版本的性能吗,因为 C++ 11 不适用于某些旧项目,因为他们正在使用旧版本的 Visual Studio?

下面是我用来测试这些的代码 sn-p。 注意。您需要在 boost 和 std 之间手动切换。 注意。 “SimpleMSTimer.hpp”是我用于提升 ptime 的计时器包装器,此处发布的时间有点长。但请随意使用您自己的计时器。任何便携式时间都可以。

#include "stdafx.h"
#include <vector>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost\make_shared.hpp>

#include "SimpleMSTimer.hpp"//my timer wrapper for boost ptime

using namespace std;
using namespace boost;

class Thing
{
public:
    Thing()
    {
    }

    void method (void)
    {
        int i = 5;
    }
};

typedef boost::shared_ptr<Thing> ThingPtr;

void processThing(Thing* thing)
{
    thing->method();
}

//loop1 and loop2 test shared_ptr in the vector container
void loop1(long long num)
{
    cout << "native raw pointer: ";
    vector<Thing> thingPtrs;
    YiUtil::MSSegmentTimer segTimer(YiUtil::MSSegmentTimer::MLSEC, std::cout);
    for(int i=0; i< num; i++) {
        Thing thing;
        thingPtrs.push_back(thing);
    }
    thingPtrs.clear();
}

void loop2(long long num)
{
    cout << "native boost::shared_ptr: ";
    vector<ThingPtr> thingPtrs;
    YiUtil::MSSegmentTimer segTimer(YiUtil::MSSegmentTimer::MLSEC, std::cout);
    for(int i=0; i< num; i++) {
        ThingPtr p1(new Thing);
        thingPtrs.push_back(p1);
    }
}

void loop3(long long num)
{
    cout << "optimized boost::shared_ptr: ";
    vector<ThingPtr> thingPtrs;

    YiUtil::MSSegmentTimer segTimer(YiUtil::MSSegmentTimer::MLSEC, std::cout);
    for(int i=0; i< num; i++) {
        ThingPtr p1 = boost::make_shared<Thing>();
        thingPtrs.push_back(p1);
    }
}


//loop3 and loop4 test shared_ptr in loop
void loop4(long long num)
{
    cout << "native raw pointer: ";
    YiUtil::MSSegmentTimer segTimer(YiUtil::MSSegmentTimer::MLSEC, std::cout);
    for(int i=0; i< num; i++) {
        Thing* p1 = new Thing();
        processThing(p1);
        delete p1;
    }
}

void loop5(long long num)
{
    cout << "native boost::shared_ptr: ";
    YiUtil::MSSegmentTimer segTimer(YiUtil::MSSegmentTimer::MLSEC, std::cout);
    for(int i=0; i< num; i++) {
        ThingPtr p1(new Thing);
        processThing(p1.get());
    }
}

void loop6(long long num)
{
    cout << "optimized boost::shared_ptr: ";
    YiUtil::MSSegmentTimer segTimer(YiUtil::MSSegmentTimer::MLSEC, std::cout);
    for(int i=0; i< num; i++) {
        ThingPtr p1 = boost::make_shared<Thing>();
        processThing(p1.get());
    }
}

int main() {
    long long num = 10000000;
    cout << "test 1" << endl;
    loop1(num);
    loop2(num);
    loop3(num);

    cout << "test 2"<< endl;
    loop4(num);
    loop5(num);
    loop6(num);

    return 0;
}

发布模式下的 VC10 编译器,gcc 使用标志 '-O3' 编译以进行最大优化。 测试结果:

//VS2010 release mode
//boost
test 1
native raw pointer: SegmentTimer: 15 milliseconds/n
native boost::shared_ptr: SegmentTimer: 3312 milliseconds/n
optimized boost::shared_ptr: SegmentTimer: 3093 milliseconds/n
test 2
native raw pointer: SegmentTimer: 921 milliseconds/n
native boost::shared_ptr: SegmentTimer: 2359 milliseconds/n
optimized boost::shared_ptr: SegmentTimer: 2203 milliseconds/n

//std
test 1
native raw pointer: SegmentTimer: 15 milliseconds/n
native std::shared_ptr: SegmentTimer: 3390 milliseconds/n
optimized std::shared_ptr: SegmentTimer: 2203 milliseconds/n
test 2
native raw pointer: SegmentTimer: 937 milliseconds/n
native std::shared_ptr: SegmentTimer: 2359 milliseconds/n
optimized std::shared_ptr: SegmentTimer: 1343 milliseconds/n
==============================================================================
gcc 4.72 release mode
//boost
test 1
native raw pointer: SegmentTimer: 15 milliseconds/n
native boost::shared_ptr: SegmentTimer: 4874 milliseconds/n
optimized boost::shared_ptr: SegmentTimer: 3687 milliseconds/n
test 2
native raw pointer: SegmentTimer: 1109 milliseconds/n
native boost::shared_ptr: SegmentTimer: 2546 milliseconds/n
optimized boost::shared_ptr: SegmentTimer: 1578 milliseconds/n

//std
test 1
native raw pointer: SegmentTimer: 15 milliseconds/n
native std::shared_ptr: SegmentTimer: 3374 milliseconds/n
optimized std::shared_ptr: SegmentTimer: 2296 milliseconds/n
test 2
native raw pointer: SegmentTimer: 1124 milliseconds/n
native std::shared_ptr: SegmentTimer: 2531 milliseconds/n
optimized std::shared_ptr: SegmentTimer: 1468 milliseconds/n

【问题讨论】:

  • 您运行了多少次基准测试?您是否尝试更改基准测试的顺序?你看过 std::shared_ptr 和 boost::shared_ptr 的来源吗?
  • 这不是任何基准测试,您应该避免对这些测试做出判断。
  • boost 1.47 已经很老了,试试boost 1.51,应该会支持c++11。
  • @all:是的,我确实运行了好几次,不同之处似乎非常一致。免费免费运行它们只需提供您自己的计时器。
  • @Jesse Good:我确实检查了 boost 智能指针历史记录,它已经有一段时间没有真正更新了,从我运行的 1.47 开始就没有。

标签: c++ performance visual-studio gcc boost


【解决方案1】:

它们的性能要好得多,因为 Boost 版本没有更新为使用右值引用,这启用了移动语义。而 C++11 版本确实使用移动语义。这意味着 Boost 版本必须更频繁地复制。如果您在 C++11 之前的编译器上进行测试,您的目标库(使用 std::tr1::shared_ptr)应该执行得更加相似。

【讨论】:

  • 我认为使用移动语义几乎可以肯定是性能更好的原因,但是 shared_ptr 不会复制指向的对象-它只是更新引用计数,这有点时间-当您必须以线程安全的方式执行此操作时消耗。
  • @DeadMG:我一直在考虑移动语义效果,但就是这样吗?同样在我的问题中,我问是否有可能使 boost shared_ptr、make_shared 具有更好/匹配的性能作为 c++ std 版本,你也能回答这个问题吗?
  • 这肯定是不可能的,除非你想分叉库并自己实现移动语义。是的,很可能就是这样。
猜你喜欢
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 2018-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多