Re: Don't pass by reference to non-const?
On 3 May, 04:43, "Alf P. Steinbach" <al...@start.no> wrote:
On 03.05.2010 03:11, * Ian Collins:
On 05/ 3/10 10:42 AM, Balog Pal wrote:
"James Kanze" <james.ka...@gmail.com>
And sometimes, performance issues raise their ugly
head---in my current work, we do use out parameters of
things like std::vector<double>, because returning
a vector with a couple of thousand elements was too slow.
You mean you had a situation wanting performance yet you
used a compiler that did so poor job on optimization that
retutning a vector was actually slower than passing in an
empty one by reference?
I love it when people comment about things they know nothing
about. We did the measures with a number of different
compilers: VC++, g++, Intel and Sun CC. In all cases, passing
the pre-constructed vector to the function was significantly
faster than returning a vector.
I was about to post the same comment (until some prawn in
a digger hit our power cable!). I ran a quick test and found
passing by reference slower than returning a vector of 10000
doubles. I had expected them to be about the same.
When you do it manually instead of RVO you have an extra
default construct construction and an extra swap, but it
shouldn't really matter for efficiency.
The compiler can't always use RVO. Our two versions were:
std::vector<double> v(...);
for (... lot's of iterations ... )
{
// calculate some values based on the current contents
// of v.
v = some_function(... the calculated values ...);
// ...
}
as opposed to
std::vector<double> v(...);
for (... lot's of iterations ... )
{
// calculate some values based on the current contents
// of v.
some_function(&v, ... the calculated values ...);
// ...
}
(As far as I can tell, this is a more or less standard procedure
in numerical analysis. Although in some cases, you might have
two vectors, one with the old values, and one in which you put
the new, swapping them each time you go through the loop.)
--
James Kanze