ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> boost
boost
Re: [boost] Postfix Increment Operator
by Brad King other posts by this author
Jun 12 2002 7:57PM messages near this date
Re: [boost] Postfix Increment Operator | Re: [boost] Postfix Increment Operator
| 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;
}



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Thread:
Powell, Gary
=?iso-8859-1?Q?Terje_Sletteb=F8?=
Daniel Frey
Daniel Frey
David Abrahams
Richard Peters
David Abrahams
Lars Gullik =?iso-8859-1?q?Bj=F8nnes?=
Brad King
Brad King

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved