Edit: See bottom for specifics on Vector
that is 16 bytes long.
It is very likely that the first one is significantly faster if the vector has more than a few elements (or the elements are themselves quite large).
However, "the devil is in the detail" as they say. It's possible that, under some specific circumstances, the second case is indeed faster. That would be an exception rather than the rule, but it's still a possibility.
In the second case, the vector is being copied [unless the compiler can inline the code AND the compiler can realise what is going on, and remove the extra copy]. If the vector has 10000 elements, that's 10000 copies of whatever is in the vector.
In the first case, all that is passed from the calling function to the caller function is a single pointer. On the other hand, since it's a reference, the generated code would have to make one more memory reference to read the content. So if the vector is very small, and the test
function is doing quite a few accesses to the vec
variable, it is possible that the extra overhead of the indirection is "worse" than the copy of the content.
If in doubt, benchmark the two solutions.
Make sure that the benchmark is representative - you can get it equally wrong by making it 100x faster for 10k elements, and then end up with 2x slower when the number of elements is less than 20 - and the average is 11...
Edit: Since the question was updated, I have to add that "since Vector
object is quite small", it's much less likely to be a significant difference between the choices. On a 32-bit system, the pass by reference option is likely to still have a small benefit [but, as I said in the above, it's balanced against more complex access to the Vector
content]. On a 64-bit system, it's quite possible that passing two register values is faster than a reference.
Again, benchmark under "normal" type loads.