In each article it is written that make_shared
is more efficient, than shared_ptr<T>(new T)
, because of one memory allocation not two. But I try this code:
#include <cstdio>
#include <ctime>
#include <memory>
#include <vector>
static const size_t N = 1L << 25;
int main(void) {
clock_t start = clock();
for ( size_t rcx = 0; rcx < N; rcx++ ) {
auto tmp = std::shared_ptr<std::vector<size_t>>( new std::vector<size_t>( 1024 ) );
}
clock_t end = clock();
printf("shared_ptr with new: %lf\n", ((double)end - start) / CLOCKS_PER_SEC);
start = clock();
for ( size_t rcx = 0; rcx < N; rcx++ ) {
auto tmp = std::make_shared<std::vector<size_t>>( 1024 );
}
end = clock();
printf("make_shared: %lf\n", ((double)end - start) / CLOCKS_PER_SEC);
return 0;
}
compile with:
g++ --std=c++14 -O2 test.cpp -o test
and got this result:
shared_ptr with new: 10.502945
make_shared: 18.581738
Same for boost::shared_ptr
:
shared_ptr with new: 10.778537
make_shared: 18.962444
This question has answer about LLVM's libc++ is broken, but I use libstdc++ from GNU.
So, why is make_shared
slower?
P.S. With -O3 optimization got this result:
shared_ptr with new: 5.482464
make_shared: 4.249722
same for boost::shared_ptr
.
make_shared
was faster with both-O2
and-O3
on a Linux server. What is your architecture, OS, and version of GCC, libstdc++, and glibc? – Daniel Langrmake_shared
is also faster. I getshared_ptr with new: 12.624000 make_shared: 10.969000
release/x64/cl version 19.23.28106.4 --- with release/x86 it's even faster:shared_ptr with new: 10.610000 make_shared: 7.620000
– churillmake_shared
loop run before thenew
loop? – xskxzr