Constructor Optimization
Benjamin Meyer
kde-optimize@mail.kde.org
Tue, 21 Jan 2003 12:47:59 -0500
A much better correct approce. :-D After testing that I do see that it w=
ill=20
in all cases use v(x) and not use the copy constructor. Oh well it was w=
orth=20
a shot.
-Benjamin Meyer
On Tuesday 21 January 2003 11:42 am, Zack Rusin wrote:
> On Tuesday 21 January 2003 10:47, Benjamin Meyer wrote:
> > I was curius if GCC/G++ would optimize foo =3D x down to foo(x) if th=
e
> > constructor existed so I wrote a small program to time the two. Now
> > of course the time savings couldn't be much, but a wide scale
> > fix/change could probably help, especially in applications that
> > constently generate/remove new variables.
>
> <snip>
>
> > Is this wrong?
>
> Yes. For a couple of reasons. The time measurements you did are pretty
> much useless, nothing accurate will come of them. I'd suggest writting
> a lot simpler testcase like:
>
> class Copy {
> public:
> Copy( int i =3D 0 ): mI(i) { cerr<<"\tNormal Constructor"<<endl; }
> Copy( const Copy& c ) { mI =3D c.getI();
> cerr<<"\tCopy Constructor"<<endl; =
}
> int getI() const { return mI; }
> void setI( int i ) { mI =3D i; }
>
> void operator=3D( const Copy &c ) { mI =3D c.getI();
> cerr<<"\toperator=3D"<<endl; }
> private:
> =09int mI;
> };
>
> This will show that gcc in fact always calls copy constructors when
> initializing objects (that is while the signature of one of the copy
> constructors is compatible with operator=3D)
>
> Zack