我试图测试std::swap
and的性能差异,vector::swap
我在编译时使用和不使用该-std=c++0x
选项。我注意到大约 200 毫秒的差异,当我不包含此选项时,程序运行得更快。
#include <iostream>
#include <string>
#include <vector>
int main()
{
commentator.setReportStream (cout);
size_t nbElts = 2048;
vector<int> v, w;
v.resize (nbElts);
w.reserve (nbElts);
for (int i = 0; i < nbElts; ++i) {
w.push_back (i);
}
commentator.start ("std::swap", __FUNCTION__);
for (int i = 0; i < 10000000; ++i) {
std::swap (v, w);
}
commentator.stop (MSG_DONE);
commentator.start ("vector::swap", __FUNCTION__);
for (int i = 0; i < 10000000; ++i) {
v.swap (w);
}
commentator.stop (MSG_DONE);
return 0;
}
评论者对象显示运行时间。为什么运行时间不同? gcc 版本 4.6.3 20120306 (Red Hat 4.6.3-2) (GCC)
没有 -std=c++0x 的运行时间
std::swap...done (0.319952 s)
Completed activity: std::swap (r: 0.3214s, u: 0.32s, s: 0s) done
vector::swap...done (0.26396 s)
Completed activity: vector::swap (r: 0.2652s, u: 0.264s, s: 0s) done
使用 -std=c++0x
std::swap...done (0.548917 s)
Completed activity: std::swap (r: 0.5507s, u: 0.5489s, s: 0s) done
vector::swap...done (0.508922 s)
Completed activity: vector::swap (r: 0.5105s, u: 0.5089s, s: 0s) done