
"Thorsten Ottosen" <nesotto@cs.auc.dk> writes:
"David Abrahams" <dave@boost-consulting.com> wrote in message news:uis8350ii.fsf@boost-consulting.com... | "Thorsten Ottosen" <nesotto@cs.auc.dk> writes:
| > Hm...I ran the code...it's as fast as passing the object directly to the | > function. | | Fine, show me your test and I'll show you what's wrong with it.
Sorry, I thought it was attached earlier. Here it is.
-Thorsten
Here we go:
#include <vector> #include <iostream>
std::vector<int> foo();
template< class V > long print( const V& v ) { long l = 0;
for( size_t i = 0; i != v.size(); ++i ) l += v[i];
Spending time looping here just serves to help erase the differences in time caused by the extra copy. I replaced it with: if (!v.empty()) l += v[v.size() / 2];
return l; }
template< class V > struct Foo { V v; };
#include <boost/progress.hpp>
int main() { const int sz = 10000;
On my 1GhZ PIII with most compilers, that number is fine. With Intel C++ 7/8 it seems to be too low to show the difference. Multiply it by 10.
{ boost::progress_timer t;
long l = 0; for( int i = 0; i != sz; ++i ) l += print( foo() );
std::cout << l << " ";
You're also counting the time it takes to do I/O here, which similarly serves to erase the differences in time. I/O is expensive and it can swamp the costs of allocating the new vector.
}
{ boost::progress_timer t;
long l = 0;
for( int i = 0; i != sz; ++i ) { //const std::vector<int>& v = foo(); //l += print( v ); Foo< std::vector<int> > f; f.v = foo(); l += print( f.v );
}
std::cout << l << " ";
Likewise.
}
}
std::vector<int> foo_impl() { const int sz = 3000; std::vector<int> v; for( int i = 0; i != sz; ++i ) v.push_back( i ); return v; }
std::vector<int> foo() { static std::vector<int> v( foo_impl() ); return v; }
The enclosed will show the difference on any compiler I can get my hands on. I'd be happy to show you the output if you need proof. On most compilers it's a factor of 2. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com