I wanted to know if it is faster to normally return a value from a function or to use a pointer as a parameter and pass the value to that pointer.
-
It may be slow amount it is assigned separately also return value, but the difference is insignificant. It will become larger by the size of the struct in the case just like the return structure. – BLUEPIXY Feb 10 '14 at 14:16
-
You should first consider how much you use the data object, returning a pointer means allocating on the heap, and heap memory is slower to begin with. So returning a pointer may be faster in some cases, but if using that struct/object then is slower, and you use it a lot, your micro-optimization was counter productive. Passing a pointer to a stack variable in the calling function is probably faster... but that's a generalization that might not hold true in your case – Elias Van Ootegem Feb 10 '14 at 14:21
-
1Profile your specific case. – Enigma Feb 10 '14 at 14:28
-
You have two horses. You wish to know which is faster. So you ask strangers on the internet which horse they think is faster? **Race your horses**. You can find out which one is faster by *trying it both ways with a stopwatch*. – Eric Lippert Feb 10 '14 at 23:54
5 Answers
In different common ABIs, return-by-value for large (not fitting in registers) objects is implemented through a pointer anyways. The caller reserves the space, and passes a pointer to the callee, that uses that pointer to create the object in place.

- 204,818
- 23
- 294
- 489
-
-
@FiddlingBits: It depends on the language, in C maybe (not a C expert) in C++ a `struct` and a class are the same thing and in most cases `memcpy` is not the correct thing to do and the compiler won't do it. in the Itanium ABI, a *large enough* `struct` is returned by value by means of the caller passing a hidden pointer and the callee creating the `struct` in place; as already stated in the answer. – David Rodríguez - dribeas Feb 10 '14 at 14:50
With modern Compilers and C++11 returning by value is fastest in many cases: Want Speed? Pass by Value. (Archive)
-
There's a [talk](https://www.youtube.com/watch?v=eR34r7HOU14) by Chandler Carruth that goes in depth of why it's faster to pass variables by value. – sigod Jan 23 '18 at 23:16
I will assume we're talking about C++11
here, since it's been 2.x years already.
Start by returning your object by value: move semantics, (N)RVO can kick in and generate really fast code that is really easy to read. However, if you profile your code and find that this particular function is a bottleneck, consider using a reference as an "out-parameter." This may in fact be faster than using a pointer, as the compiler has more flexibility with how to represent a reference. The ISO standard for C++ does not dictate that references require storage, so the compiler is free to make the reference a literal alias of the other memory location, using effectively zero bytes of overhead.
All in all though, write the cleanest code first, and then measure it. People underestimate just how much optimization the compiler can do for you if you just return your (movable) objects by value.

- 7,746
- 1
- 26
- 39
Pointer is 4 or 8 bytes long depending on the architecture.
If your value is less that that in size, it might be faster to pass values.
If you have large objects and copy constructors, then more memory will be copied and passing that kind of parameters would be more expensive.
But... compiler optimizations, memory alignment, and other sorcery, might need you to directly investigate this in YOUR case.

- 19,718
- 12
- 58
- 99
-
They are not asking about passing values. They are asking about whether to return values, or pass pointers as parameters. – juanchopanza Feb 10 '14 at 14:15
-
-
Usually, return values are constructed in place in C++, so there are no expensive copies made. – juanchopanza Feb 10 '14 at 16:00
-
Well, if you have a class with 20 members, malloc, zerofill (best case), return implicit pointer to it. Or am I missing something? – Daniel Mošmondor Feb 10 '14 at 17:10
-
The return value is *usually*, and subject to some conditions, constructed in place at the caller side. So there are no *copies*, just a single instantiation. – juanchopanza Feb 10 '14 at 17:12
In return by value
case, assume the caller has an object to pass the compiler will crate a copy of that argument which is more or less equivalent to the cost of pass by reference
. So from performance point of view either solutions seems equivalent.

- 12,528
- 4
- 33
- 46