Boost logo

Boost :

From: Stephen Silver (boost_at_[hidden])
Date: 2001-01-22 14:10:30


Paul Moore wrote:

> 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.

Did you look at the generated code, or are you just guessing?
If std::swap() is inlined, as is likely, then any reasonable compiler
would optimise away the unnecessary assignments for built-in types.
GCC 2.95.2 generates exactly the same code for the above two forms
of the loop when IntType is long int.

I think using std::swap() is a good idea.

Stephen


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