I was trying to test the difference in the performance of std::swap
and vector::swap
and I compiled with and without the -std=c++0x
option. I have noticed about ~200ms of difference, with the program running faster when I do not include this option.
#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;
}
The commentator object shows the running time. Why is the difference in running time?
gcc version 4.6.3 20120306 (Red Hat 4.6.3-2) (GCC)
Runing time without -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
with -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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…