Boost logo

Boost :

From: Darin Adler (darin_at_[hidden])
Date: 2001-01-22 15:36:31

on 1/22/01 11:10 AM, Stephen Silver at boost_at_[hidden] wrote:

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

I can confirm that Metrowerks CodeWarrior 6.0 also generates identical code
for the above two forms when optimization and function inlining is on.

    -- Darin

Boost list run by bdawes at, gregod at, cpdaniel at, john at