Boost logo

Boost :

From: Paul Moore (gustav_at_[hidden])
Date: 2001-01-21 17:33:27


I have a small optimisation issue - in the rational class, the gcd
operation (one of the key hotspots) repeatedly executes

    IntType r(n%m);
    n = m;
    m = r;

This could be replaced with

    IntType r(n%m);
    std::swap(n,m)
    std::swap(m,r);

For built-in types, this is notably less efficient, as it will do 6
assignments as opposed to 2. However, for a user-defined type
which may have a highly optimised swap, it could be far more
efficient.

Is there any way of using the most efficient version in each case -
or alternatively would it make sense to use the swap version in all
cases? (I feel that this is a bad case of premature optimisation, as
we don't yet have a common candidate for an unlimited-precision
integer type to use with rational<>)

[[ As usual, this could be handled by explicit specialisation - but I
don't feel that is appropriate here. ]]

This came to me when I was writing a section for the
documentation covering performance issues. If I don't use swap(), I
feel that it might be worth pointing this out in this section.

BTW, I suspect this means that rational<> should provide a swap()
specialisation, to delegate to (potential) swap() specialisations for
IntType. Can anyone remind me what the canonical form of swap()
is?

Paul


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