Is passing a char or short by reference or pointer slower than passing by value?

Viewed 218

I've always heard it said that passing by reference instead of value (copying) is more efficient for types larger than int or long long because it avoids copying. When an argument is passed by value the value is pushed onto the stack where the operations are done in the function, but the way I see it is that when you pass a reference or pointer you're passing the address of the variable. If that's the case, is it true that the CPU has to fetch that value from that address, which isn't local like stack variables, resulting in fetching data that might not be in cache? Does that mean that passing something like char or short is slower if done by reference or pointer? I've heard it said that for these types it doesn't make a difference, but if it doesn't, could you explain where my reasoning is wrong?

4 Answers

First of all, in nearly all cases 'faster' and 'slower' cannot be determined by some reasoning. It just requires measuring time for different situations.

Having said the above let's get to the chase. Here are a few important points:

  • no matter what you do compiler is free to change anything as long as it is not changing the observable behaviour of program. So it might be unimportant from this perspective since compiler can interchange (in both ways) pass by const T& and pass by T as long as it knows it makes no difference from semantic perspective
  • even if the compiler does not change anything you are likely not to observe any effect since nearly all modern processors work quite well with single level of indirection (which results in single assembler instruction). Of course not every instruction is equally fast but this gives you idea about how little this changes
  • (I think) there is a slightly bigger possiblity of cache miss when passing by reference which theoretically can incur some bigger performance penalties but I think memory prefetching is sophisticated enough on current processors to deal with this problem in most of cases
  • even if your processor has visible speed differences for these operations it still will be less important than algorithmic complexity making this a theoretical questions really, not a practical one

Yes, it is. But it doesn't cause big performance issue. Also, fetching value from memory is slower than copyying it. Also for int, long long types, you should not use references for them.

According to an older post in StackOverflow @green-lantern said:

  • Overhead with passing by reference: each access needs a dereference, i.e., there is one more memory read
  • Overhead with passing by value: the value needs to be copied on the stack or into registers

    For small objects, such as an integer, passing by value will be faster. For bigger objects (for example a large structure), the copying would create too much overhead so passing by reference will be faster.

Source:Pass by value faster than pass by reference

Let's make a quick glance at the sizes for the different types:

  • char - 1 byte
  • char pointer - 8 bytes
  • short - 2 bytes
  • short pointer - 8 bytes

As a rule of thumb, passing by reference or pointer is typically faster than passing by value, if the amount of data passed by value is larger than the size of a pointer.

In this current case passing by value is faster than passing by reference for the types char and short

Here's an analogy:

Pass by value: Here is the thing.
Pass by reference: You go to this address and you'll find the thing.    

Which is faster*? Pass by value.

Does it matter? Depends on your use case, try measuring it.

  • Totally depends on CPU architecture, cache size, and other things.
Related