I often read that unique_ptr would be preferred in most situations over shared_ptr because unique_ptr is non-copyable and has move semantics; shared_ptr would add an overhead
You're not testing anything useful here.
What you are talking about: copy
What you are testing: iteration
If you want to test copy, you actually need to perform a copy. Both smart pointers should have similar performance when it comes to reading, because good shared_ptr
implementations will keep a local copy of the object pointed to.
EDIT:
Regarding the new elements:
It's not even worth talking about speed when using debug code, in general. If you care about performance, you will use release code (-O2
in general) and thus that's what should be measured, as there can be significant differences between debug and release code. Most notably, inlining of template code can seriously decrease the execution time.
Regarding the benchmark:
unique_ptr
and naked pointers should have the same performance, it would be worth checking it, and it need not necessarily be true in debug mode.unique_ptr
batch will be affected which will perturbate the measure.You might be interested in learning more from Neil: The Joy of Benchmarks, it's not a definitive guide, but it's quite interesting. Especially the part about forcing side-effects to avoid dead-code removal ;)
Also, be careful about how you measure. The resolution of your clock might be less precise than what it appears to be. If the clock is refreshed only every 15us for example, then any measure around 15us is suspicious. It might be an issue when measuring release code (you might need to add a few turns to the loop).