Boost logo

Boost :

From: John Maddock (john_at_[hidden])
Date: 2008-07-04 05:16:19


Philippe Vaucher wrote:
>>> In general noone would write a function that took an int by const
>>> reference because doing so doesn't make sense from a performance
>>> standpoint. However often times we pass these trivial types to
>>> functions generically written to take const T&.
>>
>>
>> While I agree with it from a convenience-oriented point of view, I
>> don't think using a reference is any slower than passing the POD by
>> value.
>> My point is that it'd not make a difference if sizeof(const T&) ==
>> sizeof(T).
>> Does anyone knows of have some kind of benchmark showing passing an
>> int by const ref is actually slower than by value?

When we wrote that utility then yes we performed some benchmarks that
indicated that pass-by-value is faster: the main difference being that the
function body avoids any aliasing issues, so the compiler knows it can cache
a const value in a register, but not if the object is refered to by
reference.

However, two things have happended since then: hardware has improved to the
point where loads can be *almost* as fast as caching in a register -
especially if both pipelining and good processor cache hits are involved.
Also compilers are getting cleverer at performing the analysis required to
avoid the aliasing issue, so <shrug> your mileage may vary.

HTH, John.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk