Is it counter-productive to pass primitive types by reference? [duplicate]

You get the best performance from passing primitive types by value. This is because:

  • primitives are blitable, so the cost of the copy depends on the size
  • primitives are small, only double and long long are bigger than a reference in most environments
  • pass-by-value avoids aliasing, allowing the optimizer to really do its thing

This last point is often overlooked but can make a considerable difference.

Leave a Comment