Boost logo

Boost :

From: Brad King (brad.king_at_[hidden])
Date: 2002-06-12 14:57:55


| Why not just:
| number operator++(int)
| {
| return postfix_incrementer<number>(*this);
| }
| ?
> 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

I did some checking, and it looks like gcc 2.95 refuses to inline the
constructor-based implementation. However, gcc 3.0 will happily do so
when any optimization is used.

I just ran some tests using the original implementation I previously
posted. The test program is shown below. This is nothing formal, but
shows that even with the integer case, the new technique can beat the
traditional implementation (on this particular
platform/compiler/version/etc, at least).

It is interesting to note that the two implementations produce identical
performance when the trivial copy constructor is generated by the
compiler. The new implementation wins only when the copy constructor is
hand-implemented. I suppose this is because there are two copy
constructions with the traditional implementation, but the return value
optimization allows the new implementation to have only one copy
construction (actually 0 copy constructors and 1 special constructor).

-Brad

Several trials of each build produced equal results to two decimal places.

 Build line used: : Time from "time ./a.out"
g++304 -O3 -DNEW_IMPL -DWITH_COPY : 1.10 seconds
g++304 -O3 -DWITH_COPY : 1.16 seconds
g++304 -O3 -DNEW_IMPL : 0.39 seconds
g++304 -O3 : 0.39 seconds

Test program:

class number {
  
#ifdef NEW_IMPL
  // Use "enum" instead of "class" to make it an integer type
  // that is easier to optimize away.
  enum postfix_increment {};
  number(number& r, postfix_increment): value(r.value) { ++r; }
#endif
  
  int value;
public:
  number(): value(0) {}
  
#ifdef WITH_COPY
  number(const number& r): value(r.value) {}
#endif
  
  bool operator < (int v) const { return value < v; }
  number& operator++() { ++value; return *this; }
  
#ifdef NEW_IMPL
  const number operator++(int)
    { return number(*this, postfix_increment()); }
#else
  const number operator++(int)
    { number t(*this); ++(*this); return t; }
#endif
};

int main()
{
  number n;
  while(n++ < 0x0FFFFFFF);
  return 0;
}


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