为啥unique_ptr的移动比shared_ptr赋值要慢?
在Modern Effective C++中,提倡使用unique_ptr代替裸指针,因为unique_ptr的大小和性能与裸指针基本一致但更安全,而shared_ptr由于由原子变量的存在性能更差,但是同步赋值试下来,unique_ptr的移动很慢。
#include <memory>
#include <chrono>
#include <utility>
#include <iostream>
int main()
{
#define COUNT 100000000
using MyType = int;
decltype(auto) u = std::make_unique<MyType>();
decltype(auto) s = std::make_shared<MyType>();
decltype(auto) r = new MyType;
decltype(auto) u1 = std::move(u);
decltype(auto) s1 = s;
decltype(auto) r1 = r;
auto start = std::chrono::high_resolution_clock::now();
auto finish = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = finish - start;
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < COUNT; ++i) r = r1;
finish = std::chrono::high_resolution_clock::now();
elapsed = finish - start;
std::cout << "ptr: " << elapsed.count() << "s\n";
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < COUNT; ++i) u = std::move(u1);
finish = std::chrono::high_resolution_clock::now();
elapsed = finish - start;
std::cout << "unique_ptr: " << elapsed.count() << "s\n";
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < COUNT; ++i) s = s1;
finish = std::chrono::high_resolution_clock::now();
elapsed = finish - start;
std::cout << "shared_ptr: " << elapsed.count() << "s\n";
}
运行结果:
-O0 肯定是不行的,C++ 比速度一定要开优化。
不开优化的话,这里的 std::move 怎么说也是一个函数调用。多了一个函数调用就没法比了。
另外,你这个 unique_ptr 的测试里还多了一个 delete ,因为在第二轮循环 u1 为 nullptr ,u 就被 delete 掉了。