Meysam
Meysam

Reputation: 18167

Is there any performance boost in storing the return value of a function by reference?

Is there any performance boost in storing the return value of a function by reference? I mean is the following code preferred because it prevents copying the return value of function into the variable?

string Foo()
{
    string someString = "some string";
    return someString;
}

const string &str = Foo();


Edit: Although in the above example Foo() is returning a string, my question concerns returning other type of objects as well.

Upvotes: 2

Views: 177

Answers (5)

Steve Jessop
Steve Jessop

Reputation: 279345

Your code as it stands contains two copies in Foo:

(1) from a temporary object of type string constructed using the const char* constructor, to someString.

(2) from the variable someString to the temporary object that is the return value of Foo.

You are writing const string &str = Foo(); instead of const string str = Foo(); with the intention of preventing a third copy:

(3) from the temporary object that is the return value of Foo, to str.

All three copies are permitted by the standard to be elided, so the question basically is whether your compiler needs help with 3.

As a rule of thumb I would say no -- if your compiler is smart enough to elide copies 1 and 2, then likely it is smart enough to elide 3. If it is not smart enough to elide any of them, and this costs you performance, then you need to work on function Foo as well as on code that calls it. But this situation won't come up -- as long as you remember to turn on optimization, any real C++ compiler can do copy elision.

There could conceivably be special cases where you need to write your code carefully to help the compiler out where it fails to elide some copy that it is permitted to elide. You need to find those cases as they occur, no general coding guideline can (accurately) tell you that you need to worry about (3) but not (1) and (2).

Upvotes: 1

Nathan822
Nathan822

Reputation: 198

The way string is implemented in C++ is as an array of characters. So, when you use a reference, the primary advantage is that a member-wise copy can be avoided, and the object being pointed to is the object that has been passed in itself. This improves performance where large strings or arrays are involved. For your example, the string is really short and does not really matter! Hope this helps :).

Upvotes: 4

Denis Ermolin
Denis Ermolin

Reputation: 5556

Know, you will not gain perfomance boost. You can use std::move() to gain boost in this case.

Upvotes: 1

Andrew
Andrew

Reputation: 24846

Today compilers are smart enough and will probably use RVO (return value optimization) in such cases. So even if returning by value no copy overhead will exist

The main rule you should follow is writing a good readable code (because code is written for people) and bother with optimizations only when it is required and only after you found the bottleneck

Upvotes: 2

James Kanze
James Kanze

Reputation: 154007

No. It's possible, in fact, that there is a slight loss in performance (although I doubt it). About all you gain by using references in this case is obfuscation.

Upvotes: 3

Related Questions