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?