I decided to compare the performance of shared_ptr
in C++ when passed by const reference, by value, and by reference. Also, I compared the pros and cons of using these ways of passing an argument to a function:
const ref: 2056 ns // fastest value: 3948 ns // slowest ref: 3252 ns
const ref
: Can modify the value of the class instance but cannot change the shared_ptr itselfvalue
: Can modify the value of the class instance and change the shared_ptr (but the latter is limited to the scope of the function)ref
: Can modify the value of the class instance and change the shared_ptr (the changes are propagated globally)
In the above analysis, passing shared_ptr by const reference is the fastest.