Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Does return-by-value mean extra copies and extra overhead?

Reply
Thread Tools

Does return-by-value mean extra copies and extra overhead?

 
 
mathieu
Guest
Posts: n/a
 
      09-04-2009
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,
 
Reply With Quote
 
 
 
 
Francesco
Guest
Posts: n/a
 
      09-04-2009
On 4 Set, 15:36, mathieu <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      09-04-2009
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
 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      09-04-2009
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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
weighted mean; weighted standard error of the mean (sem) C Barrington-Leigh Python 1 09-10-2010 02:03 AM
what does "execfile" mean within profiler output and why does it not have a attached line number Rahul Python 4 04-07-2009 03:19 AM
What does <AF> mean and how do I know if my WebApp does GC too oftenor not Li Ma Java 1 03-09-2009 10:51 PM
Unordered extra copies of e-mail message sent Pete ASP .Net 4 09-06-2007 01:14 PM
What does this error mean? Does somebody intrude then force me to restart my PC? Kelvin @ SG Computer Security 4 02-06-2004 02:56 AM



Advertisments