Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Does return-by-value mean extra copies and extra overhead? (http://www.velocityreviews.com/forums/t697102-does-return-by-value-mean-extra-copies-and-extra-overhead.html)

mathieu 09-04-2009 01:36 PM

Does return-by-value mean extra copies and extra overhead?
 
Hi there,

I am reading:
http://www.parashift.com/c++-faq-lit....html#faq-10.9

And I am thinking does the opposite also apply? For instance:

template<typename T>
struct minimum : std::binary_function<T, T, T>
{
const T& operator()(const T& x, const T& y) const { return x < y?
x : y; }
};

Clearly for integer type such as int, there is not need to construct
a reference, but instead it would be faster (I would think) to simply
do:

template<typename T>
struct minimum : std::binary_function<T, T, T>
{
T operator()(T x, T y) const { return x < y? x : y; }
};

Thanks,

Francesco 09-04-2009 03:05 PM

Re: Does return-by-value mean extra copies and extra overhead?
 
On 4 Set, 15:36, mathieu <mathieu.malate...@gmail.com> wrote:
> Hi there,
>
> I am reading:http://www.parashift.com/c++-faq-lit....html#faq-10.9
>
> And I am thinking does the opposite also apply? For instance:
>
> template<typename T>
> struct minimum : std::binary_function<T, T, T>
> {
> const T& operator()(const T& x, const T& y) const { return x < y?
> x : y; }
> };
>
> Clearly for integer type such as int, there is not need to construct
> a reference, but instead it would be faster (I would think) to simply
> do:
>
> template<typename T>
> struct minimum : std::binary_function<T, T, T>
> {
> T operator()(T x, T y) const { return x < y? x : y; }
> };
>
> Thanks,


I think the actual results depend strongly on the implementation. You
could profile it and see by yourself if passing ints by value is
really faster than passing them by reference. After all, some
implementations could define int to have the same number of bits of
addresses, then you wouldn't be saving anything.

In any case, implementing a by-value template function like the above
you should ensure that it only gets used for built-in types -
otherwise, passing large objects to it will _surely_ cause significant
overhead.

Well, there are specializations for doing this. Define a by-reference
template, then specialize it by-value for int (once you have verified
that it really is faster).

Don't forget to declare your templates as inline - maybe it won't
help, but surely it won't harm.

Cheers,
Francesco

Maxim Yegorushkin 09-04-2009 03:33 PM

Re: Does return-by-value mean extra copies and extra overhead?
 
mathieu wrote:
> Hi there,
>
> I am reading:
> http://www.parashift.com/c++-faq-lit....html#faq-10.9
>
> And I am thinking does the opposite also apply? For instance:
>
> template<typename T>
> struct minimum : std::binary_function<T, T, T>
> {
> const T& operator()(const T& x, const T& y) const { return x < y?
> x : y; }
> };


There is an excellent article by David Abrahams on this subject:
http://cpp-next.com/archive/2009/08/...pass-by-value/

--
Max

Bo Persson 09-04-2009 04:25 PM

Re: Does return-by-value mean extra copies and extra overhead?
 
mathieu wrote:
> Hi there,
>
> I am reading:
> http://www.parashift.com/c++-faq-lit....html#faq-10.9
>
> And I am thinking does the opposite also apply? For instance:
>
> template<typename T>
> struct minimum : std::binary_function<T, T, T>
> {
> const T& operator()(const T& x, const T& y) const { return x < y?
> x : y; }
> };
>
> Clearly for integer type such as int, there is not need to
> construct a reference, but instead it would be faster (I would
> think) to simply do:
>
> template<typename T>
> struct minimum : std::binary_function<T, T, T>
> {
> T operator()(T x, T y) const { return x < y? x : y; }
> };
>


Any competent compiler will inline functions this simple, and make the
difference disappear. You shouldn't bother.


Bo Persson






All times are GMT. The time now is 05:00 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57