Returning by reference faster than returning by reference

John (Eljay) Love-Jensen eljay@adobe.com
Fri Dec 4 18:55:00 GMT 2009


Hi Nava,

Your subject isn't quite right.  It should be "Returning by reference faster than returning by value".

C++ has RVO ("return value optimization").

RVO is part of C++, just as the elide constructors is a part of C++.

Also note:  you get RVO even if you are -O0.  It really is part of the C++ language, even for non-optimized compiling.

So your two tests as given should have the about the same performance, since the two tests are basically doing the same thing.  And, indeed, they do have about the same performance.  As expected.

Read carefully the constraints upon when RVO can be applied.  (I don't have a URL handy.  You'll have to use your Google-fu, or read the ISO 14882 standard.)

In situations where RVO cannot be applied, then for non-trivial data types, an output reference parameter will (likely) be faster.

A lot of STL and Boost rely upon RVO for their template magic to be highly performant.

HTH,
--Eljay



More information about the Gcc-help mailing list