Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is more efficient: Return a value vs. Pass by reference?

I am currently studying how to write efficient C++ code, and on the matter of function calls, a question comes to mind. Comparing this pseudocode function:

not-void function-name () {     do-something     return value; } int main () {     ...     arg = function-name();     ... } 

with this otherwise-identical pseudocode function:

void function-name (not-void& arg) {     do-something     arg = value; } int main () {     ...     function-name(arg);     ... } 

Which version is more efficient, and in what respect (time, memory etc.)? If it depends, then when would the first be more efficient and when would the more efficient be the second?

Edit: For context, this question is limited to hardware platform-independent differences, and for the most part software too. Are there any machine-independent performance difference?

Edit: I don't see how this is a duplicate. The other question is comparing passing by reference (prev. code) to passing by value (below):

not-void function-name (not-void arg) 

Which is not the same thing as my question. My focus is not on which is the better way to pass in an argument to a function. My focus is on which is the better way to pass out a result to a variable from the outside scope.

like image 572
thegreatjedi Avatar asked Nov 30 '15 09:11

thegreatjedi


People also ask

Is pass by reference more efficient?

Pass-by-references is more efficient than pass-by-value, because it does not copy the arguments. The formal parameter is an alias for the argument.

Is it faster to pass by reference or value?

As a rule of thumb, passing by reference or pointer is typically faster than passing by value, if the amount of data passed by value is larger than the size of a pointer.

Which is best pass by value or pass by reference?

pass by reference. It doesn't matter if the parameters are primitive types, arrays, or objects, either a copy is made or an address is stored. As noted elsewhere, when objects are copied, the copy constructor is called to do the copying. Typically if you aren't going to change a variable, you use pass by value.

Which is generally more efficient a function that returns an object by reference or a function that returns an object by value?

Returning the object should be used in most cases because of an optimsation called copy elision. However, depending on how your function is intended to be used, it may be better to pass the object by reference.


2 Answers

First of all, take in account that returning an object will always be more readable (and very similar in performance) than having it passed by reference, so could be more interesting for your project to return the object and increase readability without having important performance differences. If you want to know how to have the lowest cost, the thing is what do you need to return:

  1. If you need to return a simple or basic object, the performance would be similar in both cases.

  2. If the object is so large and complex, returning it would need a copy, and it could be slower than having it as a referenced parameter, but it would spend less memory I think.

You have to think anyway that compilers do a lot of optimizations which make both performances very similar. See Copy Elision.

like image 187
arodriguezdonaire Avatar answered Oct 20 '22 00:10

arodriguezdonaire


Well, one must understand that compilation is not an easy buisness. there are many consideration taken when the compiler compiles your code.

One can't simply answer this question because the C++ standard doesn't provide standard ABI (abstract binary interface), so each compiler is allowed to compile the code whatever it likes and you can get different results in each compilation.

For example, on some projects C++ is compiled to managed extension of Microsoft CLR (C++/CX). since everything there is already a reference to an object on the heap, I guess there is not difference.

The answer is not simpler for un-managed compilations. several quaestion come to mind when I think about "Will XXX run faster then YYY?", for example:

  • Is you object deafult-constructible?
  • Does your compiler support return-value-optimization?
  • Does your object support Copy-only semantics or both copy and move?
  • Is the object packed in contigious manner (e.g. std::array) or it has pointer to something on the heap? (e.g. std::vector)?

If I give concrete example, my guess is that on MSVC++ and GCC, returning std::vector by value will be the as passing it by reference, because of r-value-optimization, and will be a bit (by few nanoseconds) faster then returning the vector by move. this may be completly different on Clang, for example.

eventually, profiling is the only true answer here.

like image 42
David Haim Avatar answered Oct 19 '22 23:10

David Haim