Boost logo

Boost :

From: Pavel Vozenilek (pavel_vozenilek_at_[hidden])
Date: 2002-10-15 16:09:55


>From: "Terje Slettebø" <tslettebo_at_[hidden]>
>>From: "Pavel Vozenilek" <pavel_vozenilek_at_[hidden]>
>>
>> operator+() can be implemented like described in
>> http://www.semantics.org/gotchas/gotcha36.pdf (Stewe Dewhurst's
Gotcha#36).
>> ...

> Yeah, but that would be cheating. :) ....
> ... In other words, getting the wrong result fast is never a bargain. :)
>
Cheating is way too strong word. It is technique that may or may not speedup
the code, e.g. :
- only one constructor is called (thus dynamically allocated members (if
any)
   need to be allocated only once and are not reallocated)
- depending on the class, some code otherwise invoked during copy can be
avoided
- it is ideal situation for RVO

> Test(const Test& lhs, const Test& rhs) : num(lhs.num + rhs.num)
> {
> std::memcpy(array,lhs.array,1024*sizeof(int));
> }
>
> And then we're back to having a copy in the code, after all.
>
If this trick is not used, memcpy() will likely be called twice: during copy
constructor
and then during operator+=().

I once modified Alexandresu'c flex_string<> and time needed for operator+()
dropped down to 50-75%, depending on string length (with VC++ 6.0).

IMHO this technique makes sense for very performance sensitive classes only
because
standard copy constructor and operator+=() need to be coded separately,
resulting in code duplication.

/Pavel


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