The topic generically says it all. Basically in a situation like this:
boost::scoped_array<int> p(new int[10]);
Is there any appreciable difference in performance between doing: &p[0] and p.get()?
I ask because I prefer the first one, it has a more natural pointer like syntax. In fact, it makes it so you could replace p with a native pointer or array and not have to change anything else.
I am guessing since get is a one liner ‘return ptr;‘ that the compiler will inline that, and I hope that it is smart enough to to inline operator[] in such a way that it is able to not dereference and then immediately reference.
Anyone know?
OK, I’ve done some basic tests as per Martin York’s suggestions.
It seems that g++ (4.3.2) is actually pretty good about this. At both -O2 and -O3 optimization levels, it outputs slightly different but functionally equivalent assembly for both
&p[0]andp.get().At -Os as expected, it took the path of least complexity and emits a call to the
operator[]. One thing to note is that the&p[0]version does cause g++ to emit a copy of theoperator[]body, but it is never used, so there is a slight code bloat if you never useoperator[]otherwise:The tested code was this (with the
#ifboth 0 and 1):