2
class X {
    public:
std::string name;
int age;

long references;
X(string n, int a) : references(0), name(n), age(a) {}
};

inline void intrusive_ptr_add_ref(X* x){
++x->references;
}

inline void intrusive_ptr_release(X* x){
if(--x->references == 0) 
    delete x;
}

int _tmain(int argc, _TCHAR* argv[])
{
time_t t=clock();
size_t rounds=1000000;

for(size_t i=0; i<rounds; i++)
{
    intrusive_ptr<X> myX(new X("Michael",40));
    myX->age++;
}
cout << "Time taken to generate " << rounds << " of intrusive_ptr is " 
    << clock()-t << endl;

t=clock();
for(size_t i=0; i<rounds; i++)
{
    boost::shared_ptr<X> myX(new X("Michael",40));
    myX->age++;
}
cout << "Time taken to generate " << rounds << " of shared_ptr is " 
    << clock()-t << endl;

t=clock();
for(size_t i=0; i<rounds; i++)
{
    std::shared_ptr<X> myX(new X("Michael",40));
    myX->age++;
}
cout << "Time taken to generate " << rounds << " of Microsoft shared_ptr is " 
    << clock()-t << endl;

t=clock();
for(size_t i=0; i<rounds; i++)
{
    boost::shared_ptr<X> myX=boost::make_shared<X>("Michael",40);
    myX->age++;
}
cout << "Time taken to generate " << rounds << " of shared_ptr using make_shared is " 
    << clock()-t << endl;

t=clock();
for(size_t i=0; i<rounds; i++)
{
    std::shared_ptr<X> myX=std::make_shared<X>("Michael",40);
    myX->age++;
}
cout << "Time taken to generate " << rounds << " of Microsoft shared_ptr using make_shared   is " 
    << clock()-t << endl;

_getche();
return 0;
}

I got below results using vs2010 for release mode.

Time taken to generate 1000000 of intrusive_ptr is 116 Time taken to generate 1000000 of shared_ptr is 175 Time taken to generate 1000000 of Microsoft shared_ptr is 182 Time taken to generate 1000000 of shared_ptr using make_shared is 176 Time taken to generate 1000000 of Microsoft shared_ptr using make_shared is 120

Seems intrusive_ptr is the fastest, but seems MS is also doig well with shared_ptr using make_shared function. But why is boost make_shared performing not as well as MS version? Anybody did a similiar test? Anything that is wrong with my test or there is something that I didn't consider?

Igor R.
  • 14,716
  • 2
  • 49
  • 83
Michael
  • 673
  • 2
  • 5
  • 23
  • 1
    MS make_shared has optimizations, afaik it does 1 mem alloc instead of 2 that boost does. – NoSenseEtAl Feb 12 '13 at 18:36
  • 2
    @NoSenseEtAl, `boost::make_shared` also only does one allocation. **All** known implementations of `make_shared` only do one allocation, it's not an MS-specific thing (Boost and GCC both implemented it long before MS) – Jonathan Wakely Feb 14 '13 at 00:40
  • @JonathanWakely It could be atm but before VS had an optimization that Boost didnt have. http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/STL11-Magic-Secrets – NoSenseEtAl Feb 14 '13 at 07:57
  • 1
    @NoSenseEtAl, no, you've misunderstood the video. That "we know where you live" optimization stores one less pointer in the control block, reducing the memory footprint, it does **not** do fewer allocations. Boost had `make_shared` before MS had it, and it always did one allocation. I wrote GCC's `make_shared` before MS had it, and it always did one allocation. Only MS has the "we know where you live" optimization, but it is **not** about number of allocations, it's about how many bytes are allocated in the one allocation. – Jonathan Wakely Feb 14 '13 at 09:17
  • ok, tnx for the clarification, I apologize... – NoSenseEtAl Feb 14 '13 at 09:23

0 Answers0