2

I was reading this but now I am confused with efficiency of two ways to pass parameters.

Compared with pass by value, this tutorial told me that pass by reference is fast "because a copy of the argument is not made". However, it also told me that "dereferencing a pointer(reference) is slower than accessing it directly, accessing values passed by reference is slower than accessing values passed by value".

So here is my question: if pass by reference faster or slower than pass by value?

Thanks!

wking
  • 1,293
  • 1
  • 16
  • 27
  • on the cost of making the copy of the value. Balanced against the cost of having to dereference a pointer to obtain the value. If you have no idea then experiment with a profiler. – Hans Passant Sep 13 '14 at 12:15

1 Answers1

1

It depends on the size of passed variable.

If it's in example char then it's faster to make a copy cause it usually takes 1 B, when reference on x32 can take around 4 B on x32,
but when you pass in example a std::string, situation is the opposite - AFAIK maximum string's size is limited only by your system's architecture and your hardware.

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52