[std-interval] C++ interval std
eliasen at mindspring.com
Wed Apr 5 00:20:27 PDT 2006
George Corliss wrote:
>> Lawrence feels strongly that the standard should be to pass by
>> value. Intervals are small data structures; by value allows them
>> to be passed in registers for speed. That is how the current Sun
>> C++ compiler works. There is no reason to follow the C++ complex
>> standard. The library is often taken as an example by coders, and
>> there is no good reason to follow the tradition of C from 20 years
>> ago that structures should be passed by reference.
Sylvain Pion wrote:
> I don't have any strong opinion on this.
> I only did a very local experiment on some code of mine on x86, and
> changing to pass-by-value slowed the code down a bit.
I was curious to see about the speed difference between passing by
value and passing by reference, because passing by value always has been
slower and always *has* to be slower because it (unnecessarily) creates
a copy of the object when passed in. I tried three ways on x86_64, gcc
addByValue: 2230000 ticks
addByRef: 1850000 ticks
addByConstRef: 1880000 ticks
addByValue: 870000 ticks
addByRef: 550000 ticks
addByConstRef: 510000 ticks
addByValue: 720000 ticks
addByRef: 410000 ticks
addByConstRef: 440000 ticks
addByValue: 1110000 ticks
addByRef: 600000 ticks
addByConstRef: 610000 ticks
There's a bit of noise in the numbers, but passing by value is always
significantly slower. These tests were run repeatedly and the
proportions are always the same. -O3 is actually consistently slower,
which shouldn't surprise anyone who has worked with performance tuning
in gcc. It usually is.
Also ran on x86 Windows. Pattern was the same. With -O2, passing by
value took about 2.6 times as long!
I didn't test passing by pointer, but I can.
Passing in unmodifiable objects as constant references when
appropriate both obviates the need for copying and gives explicit
const-correctness from the start. We don't have to worry about
dereferencing pointers or memory ownership issues. It's indeed the
pattern followed by most modern libraries.
To the caller of the functions, it just looks like an ordinary
pass-by-value, but is faster, and allows the compiler to catch a lot of
types of problems.
I recommend that for long-term performance and const-correctness,
that objects be passed as constant references when possible.
Alan Eliasen | "When trouble is solved before it
eliasen at mindspring.com | forms, who calls that clever?"
http://futureboy.us/ | --Sun Tzu
More information about the Std-interval