Constructor Optimization

George Staikos kde-optimize@mail.kde.org
Tue, 21 Jan 2003 11:04:34 -0500


A better approach would be to use gcc -S and see what the assembler output is.  
gettimeofday() is horrible for testing this kind of thing, and furthermore 
when I run this several times, I get huge varying results in both directions.  


On Tuesday 21 January 2003 10:47, Benjamin Meyer wrote:
> I was curius if GCC/G++ would optimize foo = x down to foo(x) if the
> 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.
>
> Here was my result:
> v("x") is faster then v = "x": 24microseconds
>
> This was compiled with:
> g++ -c -pipe -DQWS -fno-exceptions -fno-rtti -Wall -W -O2
> -fno-default-inline -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 = 1000;
>   timeval t1, t2, t3;
>   gettimeofday(&t1, NULL);
>   for(int i = 0; i < j; i++)
>     QString v = "Hello World!";
>   gettimeofday(&t2, NULL);
>   for(int i = 0; i < j; i++)
>     QString v("Hello World!");
>   gettimeofday(&t3, NULL);
>
>   long firstLoop = (t2.tv_usec - t1.tv_usec);
>   long secondLoop = (t3.tv_usec - t2.tv_usec);
>   if(firstLoop < secondLoop)
>     qDebug("v = \"x\" is faster then v(\"x\"): %dmicroseconds", (secondLoop
> - firstLoop));
>   else
>     qDebug("v(\"x\") is faster then v = \"x\": %dmicroseconds", (firstLoop
> - secondLoop));
>
>   return 0;
> }
>
> _______________________________________________
> Kde-optimize mailing list
> Kde-optimize@mail.kde.org
> http://mail.kde.org/mailman/listinfo/kde-optimize

-- 

George Staikos