0
我想学习移动语义和我读了一招可能比一个副本更快。不过,我看恰恰相反以下琐碎代码:为什么std :: string的拷贝构造函数看起来比它的移动对象更快?
for (int i = 0; i < 100000000; ++i) {
std::string a("Copy");
std::string b = a;
}
for (int i = 0; i < 100000000; ++i) {
std::string a("Move");
std::string b = std::move(a);
}
这里是它发生在我的Mac时间:
$ time ./copy.out
real 0m2.511s
user 0m2.481s
sys 0m0.011s
$ time ./move.out
real 0m3.993s
user 0m3.933s
sys 0m0.020s
是,对于基准整个代码?编译器标志? –
我认为你的字符串太短以至于无法展示真正的差异。尝试使用不同长度的字符串 –
@VittorioRomeo,我使用铿锵与c + + 14和禁用优化(O0)。 – Zaxter