[std-interval] C++ interval std
Steve Clamage
Stephen.Clamage at Sun.COM
Wed Apr 5 05:51:32 PDT 2006
Alan Eliasen wrote:
> Bill Clarke wrote:
>
>>This experiment is extremely platform specific, as you probably are
>>aware. Most platforms in use today other than x86 will be the other way
>>around (add-by-value will be faster than add-by-const-ref).
>
>
> How can that assertion possibly be made? As you know, if a parameter
> is passed by value, then the copy constructor is automatically called
> for each object (e.g. twice for an add operator) on function entry, and
> then the destructor is called (twice) on function exit.
A simple object like complex or interval should have no user-defined copy
constructor or destructor. That is, the default copy constructor generated by
the compiler does the right thing and does not need to be user-defined. The
destructor has nothing to do, and so should not be user-defined. In that case,
pass-by-value can be optimized into register passing with no additional copying.
It is possible to construct a test case where pass-by-value gives worse
performance than pass-by-reference, and your example does indeed run faster with
pass-by-reference using Sun compilers at low optimization.
At high optimization, the compiler eliminated the loops and all but one call to
"add", which it inlined. I played with the test case for a short time, but was
unable to defeat the optimizer with minor changes.
I will need a little time to construct a test case that forces the compiler to
retain the loops and call the add functions. I'm at the C++ Committee meeting
right now, and don't have much time to work on this test.
---
Steve Clamage, stephen.clamage at sun.com
More information about the Std-interval
mailing list