Technically, the second can be faster because the first is subject to aliasing - there's no way to tell whether pInt
isn't modified outside the function. Just because pInt
is const
inside the method, it doesn't mean the original variable passed as argument has to be const
- remember non-const
to const
conversion is implicit.
A workaround for this issue would be using _restrict
if supported by the compiler. Note that this is an intrinsic and not part of the language.
In most usage though, they'll be the same. Profile profile profile. Write the code for readability, and only do these small optimizations when and if you know they're worth it.
This applies to int
- if you're passing a a large structure, passing by value can be a bottleneck if copy elision can't be applied to that particular case. So passing a pointer would be faster, but I'd still use a reference.