ptr vs ref performance?

Nathan Myers ncm@nospam.cantrip.org
Thu Dec 14 19:10:00 GMT 2000


On Thu, Dec 14, 2000 at 04:37:48PM -0800, Rob Willis wrote:
> In the process of playing with the performance of "long long" types, i
> noticed another oddity.  It appears as if using a reference variable is
> faster than de-referencing from a pointer.  My assumption would of been
> that the time for these operations would be exactly the same, but
> obviously there is something more going on in ptr dereferences...

When memory gets written to, the compiler must often assume that any 
pointers that live there might have been clobbered, and have to be
reloaded.  For references, the compiler can optimize based on an 
invariant: references cannot legally be changed.  Therefore, the 
compiler can assume that its in-register copy of the reference is 
the same as the definitive memory version, even though some memory 
has been written to.

Nathan Myers
ncm at cantrip dot org


More information about the Libstdc++ mailing list