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

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.

Leave a Comment