I have a little doubt concerning what I understand about make_shared performance (boost or stl), so I wanted some opinion.
Working on an app in C++ I had to do some performance tests, and I ended up to compare make_shared and shared_ptr+new (note that it's not the performance improvement purpose, and i'm not expecting to gain time here, but i'm just curious now)
I use
- Debian Jessy x64
- liboost 1.55
- gcc 4.9.2
I read that make_shared is more efficient, and according to the explications I can find (allocation numbers, overhead) it seems logical to me (as far as I can understand).
But doing a quick and stupid test, I don't understand what I get
std::shared_ptr<MyUselessClass> dummyPtr = std::shared_ptr<MyUselessClass>(new MyUselessClass());
auto start = boost::chrono::high_resolution_clock::now() ;
// - STD Share
std::shared_ptr<MyUselessClass> stdSharePtr = std::shared_ptr<MyUselessClass>(new MyUselessClass());
auto stdSharePtrTime_1 = boost::chrono::high_resolution_clock::now() ;
// - STD Make
std::shared_ptr<MyUselessClass> stdMakePtr = std::make_shared<MyUselessClass>();
auto stdMakePtrTime_2 = boost::chrono::high_resolution_clock::now() ;
// - BOOST Share
boost::shared_ptr<MyUselessClass> boostSharePtr = boost::shared_ptr<MyUselessClass>(new MyUselessClass());
auto boostSharePtrTime_3 = boost::chrono::high_resolution_clock::now() ;
// - BOOST Make
boost::shared_ptr<MyUselessClass> boostMakePtr = boost::make_shared<MyUselessClass>();
auto boostMakePtrTime_4 = boost::chrono::high_resolution_clock::now() ;
boost::chrono::nanoseconds stdShare = boost::chrono::duration_cast<boost::chrono::nanoseconds>(stdSharePtrTime_1-start) ;
boost::chrono::nanoseconds stdMake = boost::chrono::duration_cast<boost::chrono::nanoseconds>(stdMakePtrTime_2-stdSharePtrTime_1) ;
boost::chrono::nanoseconds boostShare = boost::chrono::duration_cast<boost::chrono::nanoseconds>(boostSharePtrTime_3-stdMakePtrTime_2) ;
boost::chrono::nanoseconds boostMake = boost::chrono::duration_cast<boost::chrono::nanoseconds>(boostMakePtrTime_4-boostSharePtrTime_3) ;
cout << "---" << endl ;
cout << "STD share " << stdShare << endl ;
cout << "BOOST share " << boostShare << endl ;
cout << "STD make " << stdMake << endl ;
cout << "BOOST make " << boostMake << endl ;
MyUselessClass is a simple class with 3 class attributes (sting, bool, int), and only constructor and desctructor.
The "results" (I quote because it's not an accurate test, of course) are the following (i runned it into a loop to get many iterations, gives in average the same results) :
STD share 162 nanoseconds
BOOST share 196 nanoseconds
STD make 385 nanoseconds
BOOST make 264 nanoseconds
If I believe my test, make_shared is slightly slower than calling share_ptr & new instanciation. I would have expected, if I were to see any differences as nanoseconds precision, the contrary ...
So now I am wondering:
- Maybe my test is too stupid, nanosecons order has no importance in those operations ?
- Maybe I missed a point in the explanation of better performance of make_shared ?
- Maybe I missed some(serveral) point(s) in the test ?
If you have some answers of the points below, please don't hesitate :)
Thanx