Boost logo

Boost :

From: Brad King (brad.king_at_[hidden])
Date: 2002-06-11 10:14:08


> When testing this and the prev. version with gcc, I get a 2:3
> perfomance difference in favour of the traditional solution.
>
> Gcc 3.1 -O3
> ./postincr
> Number1: 6650000
> Number2: 9900000

Compilers are good at optimizing integer operations, so I'm not surprised.
There may be an improvement, however, when copy construction is more
expensive than an integer assignment.

There are other advantages than just performance, though. Doug Gregor
pointed out to me that if this occurs:

  const number operator++(int) {
    number temp(*this);
    ++(*this);
    return temp; // exception thrown on second copy construction
  }

The original value will not be "unincremented", so only the basic
exception guarantee is satisfied. However, with this implementation:

  const number operator++(int) {
    return number(*this, postfix_increment());
  }

the original value is not incremented until after the construction
finishes. With the return value optimization present, this satisfies the
strong exception guarantee.

-Brad


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