Constructor Optimization
Benjamin Meyer
kde-optimize@mail.kde.org
Tue, 21 Jan 2003 10:47:36 -0500
I was curius if GCC/G++ would optimize foo =3D x down to foo(x) if the=20
constructor existed so I wrote a small program to time the two. Now of=20
course the time savings couldn't be much, but a wide scale fix/change cou=
ld=20
probably help, especially in applications that constently generate/remove=
new=20
variables.
Here was my result:
v("x") is faster then v =3D "x": 24microseconds
This was compiled with:=20
g++ -c -pipe -DQWS -fno-exceptions -fno-rtti -Wall -W -O2 -fno-default-in=
line=20
-DNO_DEBUG
Is this wrong?
-Benjamin Meyer
---
#include <qstring.h>
#include <sys/time.h>
#include <time.h>
int main( int argc, char **argv ){
int j =3D 1000;
timeval t1, t2, t3;
gettimeofday(&t1, NULL);
for(int i =3D 0; i < j; i++)
QString v =3D "Hello World!";
gettimeofday(&t2, NULL);
for(int i =3D 0; i < j; i++)
QString v("Hello World!");
gettimeofday(&t3, NULL);
=20
long firstLoop =3D (t2.tv_usec - t1.tv_usec);
long secondLoop =3D (t3.tv_usec - t2.tv_usec);
if(firstLoop < secondLoop)
qDebug("v =3D \"x\" is faster then v(\"x\"): %dmicroseconds", (second=
Loop -=20
firstLoop));=20
else
qDebug("v(\"x\") is faster then v =3D \"x\": %dmicroseconds", (firstL=
oop -=20
secondLoop));
=20
return 0;
}