为啥unique_ptr的移动比shared_ptr赋值要慢?-灵析社区

无敌英俊大师兄

为啥unique_ptr的移动比shared_ptr赋值要慢? 在Modern Effective C++中,提倡使用unique_ptr代替裸指针,因为unique_ptr的大小和性能与裸指针基本一致但更安全,而shared_ptr由于由原子变量的存在性能更差,但是同步赋值试下来,unique_ptr的移动很慢。 #include #include #include #include int main() { #define COUNT 100000000 using MyType = int; decltype(auto) u = std::make_unique(); decltype(auto) s = std::make_shared(); 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 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"; } 运行结果: ![image.png](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241011/52bfcd2cf2fbdf9488c3412b47f92130.png)

阅读量:197

点赞量:0

问AI
-O0 肯定是不行的,C++ 比速度一定要开优化。 不开优化的话,这里的 std::move 怎么说也是一个函数调用。多了一个函数调用就没法比了。 .L51: mov rax, QWORD PTR [rbp-48] mov rdi, rax call std::remove_reference >&>::type&& std::move >&>(std::unique_ptr >&) mov rdx, rax lea rax, [rbp-120] mov rsi, rdx mov rdi, rax call std::unique_ptr >::operator=(std::unique_ptr >&&) add DWORD PTR [rbp-24], 1 .L50: cmp DWORD PTR [rbp-24], 999999 jle .L51 另外,你这个 unique_ptr 的测试里还多了一个 delete ,因为在第二轮循环 u1 为 nullptr ,u 就被 delete 掉了。