小编gus*_*f r的帖子

(缺少)C++ 11移动语义的性能改进

我已经写了很长一段时间的C++ 11代码了,并没有对它进行任何基准测试,只是期望像移动语义这样的矢量操作现在"只是更快".因此,当实际使用GCC 4.7.2和clang 3.0(Ubuntu 12.10 64位上的默认编译器)进行基准测试时,我得到了非常不满意的结果.这是我的测试代码:

编辑:至于张贴@DeadMG和@ronag的(好)的答案,我从改变的元素类型std::string,以my::string不具有swap(),并且所有的内部串大(200-700字节),因此他们不应该SSO 的受害者.

编辑2: COW是原因.由伟大的意见再次改编代码,更改从存储std::stringstd::vector<char>和离开了复制/移动onstructors(让编译器生成它们来代替).没有COW,速度差异实际上是巨大的.

编辑3:编译时重新添加以前的解决方案-DCOW.这使得内部存储成为std::string而不是std::vector<char>@chico所要求的.

#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <functional>

static std::size_t dec = 0;

namespace my { class string
{
public:
    string( ) { }
#ifdef COW
    string( const std::string& ref ) : str( ref ), val( dec % 2 ? - ++dec : ++dec ) …
Run Code Online (Sandbox Code Playgroud)

c++ gcc clang move-semantics c++11

22
推荐指数
1
解决办法
4409
查看次数

标签 统计

c++ ×1

c++11 ×1

clang ×1

gcc ×1

move-semantics ×1