By value:
#include <math.h>
#include <iostream>
#include <vector>
using namespace std;
vector<size_t> test() {
vector<size_t> t;
for(size_t n=0;n<100;n++) {
t.push_back(rand());
}
return t;
}
int main() {
size_t sum=0;
for(size_t n=0;n<10000000;n++){
vector<size_t> t = test();
for(size_t n=0;n<t.size();n++) {
sum = sum + t[n];
}
}
cout << sum << endl;
}
By reference:
#include <math.h>
#include <iostream>
#include <vector>
using namespace std;
void test(vector<size_t> &t) {
for(size_t n=0;n<100;n++) {
t.push_back(rand());
}
}
int main() {
size_t sum=0;
for(size_t n=0;n<10000000;n++){
vector<size_t> t;
test(t);
for(size_t n=0;n<t.size();n++) {
sum = sum + t[n];
}
}
cout << sum << endl;
}
Here are my timings on an Intel Core2Duo:
By value:
real 0m34.122s
user 0m33.219s
sys 0m0.436s
By reference:
real 0m34.147s
user 0m33.308s
sys 0m0.512s
So in this case the user times seem to be more or less the same. What's happening is the return value being optimized out as expected? Is there something else I should take in to account in my test?
i686-apple-darwin10-g++-4.2.1 was used.
http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/
Also interesting comment on gcc mailing list:
Your subject isn't quite right. It should be "Returning by reference faster than returning by value".
C++ has RVO ("return value optimization").
RVO is part of C++, just as the elide constructors is a part of C++.
Also note: you get RVO even if you are -O0. It really is part of the C++ language, even for non-optimized compiling.
So your two tests as given should have the about the same performance, since the two tests are basically doing the same thing. And, indeed, they do have about the same performance. As expected.
Read carefully the constraints upon when RVO can be applied. (I don't have a URL handy. You'll have to use your Google-fu, or read the ISO 14882 standard.)
In situations where RVO cannot be applied, then for non-trivial data types, an output reference parameter will (likely) be faster.
A lot of STL and Boost rely upon RVO for their template magic to be highly performant.
No comments:
Post a Comment