You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@harmony.apache.org by Enrico Migliore <en...@fatti.com> on 2005/10/10 09:10:04 UTC

optimization for speed in win32

Hi,

I did some tests  in order to see which, among __fastcall, __stdcall, 
__cdecl, __inline,
gives the fastest execution time:  my_functions() was called 300000000 times

Processor = Intel Pentium 1.4 GHz
OS = Windows 2000
Compiler = Microsoft Visual C
Executable type = release

---------------------- code ------------------------------
#include <stdio.h>
#include <time.h>

/*
 *  qualifier: __fastcall, __stdcall, __cdecl, __inline
 */
qualifier int my_function (int a, char *buf)
{

    int  int_1 = 1987;
    char char_1 = 'a';
    long long_1 = 123456789L;
    long long_2 = &long_1;

    if (a > 0)
    {
        a++;
    }
    else
    {
        a--;
    }
    if (int_1 > 9)
    {
        char_1++;
    }
    else
    {
        char_1--;
    }
    *long_2++;
    if (buf == NULL)
    {
        return -1;
    }
    buf++;
    return a;
}


double test (void)
{

    long i;
    int result;
    char buf[8];
    clock_t start;
    clock_t stop;
    double  duration;

    buf[0] = 0;

    start = clock();    
    for (i = 0; i < 300000000; i++)
    {
         result = my_function(10,buf);
    }
    stop = clock();
    duration = (double) (stop - start) / CLOCKS_PER_SEC;

    return duration;
}


Results:

--------------------------------------------------------
 qualifier          |  test duration (maximize for speed = OFF)
--------------------------------------------------------
 __fastcall       |     14.3900 seconds
 __stdcall        |     12.9700 seconds
 __cdecl (**)  |     13.6200 seconds
 __inline          |      12.9600 seconds

--------------------------------------------------------
 qualifier          |  test duration (maximize for speed = ON)
--------------------------------------------------------
__fastcall        |      5.9400 seconds
__stdcall         |       6.5300 seconds
__cdecl (**)   |       6.4800 seconds
__inline           |       0.0000 seconds (*)

(*) suspicious
(**) __cdecl is the default qualifier



Conclusion:

__inline and __fastcall give the best results
when the compiler is instructed to generate code
with the flag "optimize for speed" enabled.

I also noticed that parameters marshalling is not an issue
because the compiler will link the appropriate static library according
to the optimization selected. Therefore, in win32, we should
be able to optimize our C code for speed  without problems.

Enrico

Re: C compilers comparison: MSVC vs GCC vs DevCpp

Posted by Enrico Migliore <en...@fatti.com>.
Florian Weimer wrote:

>* Enrico Migliore:
>
>  
>
>>the code is a simple function that gets called 300000000 times.
>>    
>>
>
>There's an explicit check in GCC that prevents the removal of empty
>loops (because they are sometimes used for their timing effect on
>embedded targets, IIRC).  Therefore, your test is bogus.
>
>  
>
Hi Florian,

to make an exhaustive comparison of the various tool chains I'd need a 
test suite.
and yet, I never pretended that the test proposed was the one to use to 
select
a compiler rather than another.

I just wanted to see how the tested compilers build up the stack frame 
of function calls,
with and without optimization and the impact on the execution speed.

Enrico
  



Re: C compilers comparison: MSVC vs GCC vs DevCpp

Posted by Florian Weimer <fw...@deneb.enyo.de>.
* Enrico Migliore:

> the code is a simple function that gets called 300000000 times.

There's an explicit check in GCC that prevents the removal of empty
loops (because they are sometimes used for their timing effect on
embedded targets, IIRC).  Therefore, your test is bogus.

Re: C compilers comparison: MSVC vs GCC vs DevCpp

Posted by Enrico Migliore <en...@fatti.com>.
Tanuj Mathur wrote:

>Hi Enrico,
>  Could you provide a link to the code you used to perform these
>tests? i'd like to replicate the results for MSVC6, and then compare
>it with MSVC 7.1 and 8 (VS 2003 and VS 2005 Beta respectively). MSVC6
>is a very old compiler (1997/98), and since the C++ compiler for MSVC
>2003 (7.1) is available free of cost, that would be a way better
>compiler to target.
>
>Thanks,
>Tanuj
>
>  
>
Hi Tanuj,

 the code is a simple function that gets called 300000000 times.

-------------------------------------------------------------------------
#include <stdio.h>
#include <time.h>

int my_function (int a, char *buf)
{

   int  int_1 = 1987;
   char char_1 = 'a';
   long long_1 = 123456789L;
   long long_2 = &long_1;

   if (a > 0)
   {
       a++;
   }
   else
   {
       a--;
   }
   if (int_1 > 9)
   {
       char_1++;
   }
   else
   {
       char_1--;
   }
   *long_2++;
   if (buf == NULL)
   {
       return -1;
   }
   buf++;
   return a;
}


void main (void)
{

   long i;
   int result;
   char buf[8];
   clock_t start;
   clock_t stop;
   double  duration;

   buf[0] = 0;

   start = clock();
   for (i = 0; i < 300000000; i++)
   {
        result = my_function(10,buf);
   }
   stop = clock();
   duration = (double) (stop - start) / CLOCKS_PER_SEC;

   printf("the test lasted for %2.4f seconds\n\r",duration);
}
-------------------------------------------------------------------------

Enrico

Re: C compilers comparison: MSVC vs GCC vs DevCpp

Posted by Tanuj Mathur <ta...@gmail.com>.
Hi Enrico,
  Could you provide a link to the code you used to perform these
tests? i'd like to replicate the results for MSVC6, and then compare
it with MSVC 7.1 and 8 (VS 2003 and VS 2005 Beta respectively). MSVC6
is a very old compiler (1997/98), and since the C++ compiler for MSVC
2003 (7.1) is available free of cost, that would be a way better
compiler to target.

Thanks,
Tanuj

On 10/17/05, Enrico Migliore <en...@fatti.com> wrote:
> Hi,
>
>     I did some tests  in order to see which, among MSVC,GCC and DevCpp
> compilers yield the code with the best speed performance.
>
> The test is a function that contains pure ANSI C code (no __fastcall or
> similar)
> and doesn't call any system call of the underlying OS.
>
>
>                             MSVC               GCC                   DevCpp
> --------------------------------------------------------------------
> Arch                    Pentium               Pentium                Pentium
>
> OS                      Win2000            Linux (Knoppix)   Win2000
>
> Compiler                 6.0                     3.4
>    5.0
> version
>
> speed                  13.4 sec              14.27 sec            14.61 sec
> optimization
> off
>
> speed                  6.48 sec               0.9 sec
> 1.43 sec
> optimization
> on
>
> command line     cl  /O2 main.c     gcc -O3 main.c        (*)
> when optimiz.
> on
> --------------------------------------------------------------------
>
> (*) I didn't write it down when I did the tests
>
>
> Unless I'm doing something wrong with MSVC, it seems that
> DevCpp and GCC are almost an order of magnitude faster than MSVC.
>
>
> Enrico
>
>
>

C compilers comparison: MSVC vs GCC vs DevCpp

Posted by Enrico Migliore <en...@fatti.com>.
Hi,

    I did some tests  in order to see which, among MSVC,GCC and DevCpp
compilers yield the code with the best speed performance.

The test is a function that contains pure ANSI C code (no __fastcall or 
similar)
and doesn't call any system call of the underlying OS.


                            MSVC               GCC                   DevCpp
--------------------------------------------------------------------
Arch                    Pentium               Pentium                Pentium

OS                      Win2000            Linux (Knoppix)   Win2000

Compiler                 6.0                     3.4                     
   5.0
version

speed                  13.4 sec              14.27 sec            14.61 sec
optimization
off

speed                  6.48 sec               0.9 sec                
1.43 sec
optimization
on

command line     cl  /O2 main.c     gcc -O3 main.c        (*)
when optimiz.
on
--------------------------------------------------------------------

(*) I didn't write it down when I did the tests


Unless I'm doing something wrong with MSVC, it seems that
DevCpp and GCC are almost an order of magnitude faster than MSVC.


Enrico



Re: optimization for speed in win32

Posted by Ben Laurie <be...@algroup.co.uk>.
Blast from the past here...

Enrico Migliore wrote:
> Hi,
> 
> I did some tests  in order to see which, among __fastcall, __stdcall,
> __cdecl, __inline,
> gives the fastest execution time:  my_functions() was called 300000000
> times
> 
> Processor = Intel Pentium 1.4 GHz
> OS = Windows 2000
> Compiler = Microsoft Visual C
> Executable type = release
> 
> ---------------------- code ------------------------------
> #include <stdio.h>
> #include <time.h>
> 
> /*
> *  qualifier: __fastcall, __stdcall, __cdecl, __inline
> */
> qualifier int my_function (int a, char *buf)
> {
> 
>    int  int_1 = 1987;
>    char char_1 = 'a';
>    long long_1 = 123456789L;
>    long long_2 = &long_1;
> 
>    if (a > 0)
>    {
>        a++;
>    }
>    else
>    {
>        a--;
>    }
>    if (int_1 > 9)
>    {
>        char_1++;
>    }
>    else
>    {
>        char_1--;
>    }
>    *long_2++;
>    if (buf == NULL)
>    {
>        return -1;
>    }
>    buf++;
>    return a;
> }
> 
> 
> double test (void)
> {
> 
>    long i;
>    int result;
>    char buf[8];
>    clock_t start;
>    clock_t stop;
>    double  duration;
> 
>    buf[0] = 0;
> 
>    start = clock();       for (i = 0; i < 300000000; i++)
>    {
>         result = my_function(10,buf);
>    }
>    stop = clock();
>    duration = (double) (stop - start) / CLOCKS_PER_SEC;
> 
>    return duration;
> }
> 
> 
> Results:
> 
> --------------------------------------------------------
> qualifier          |  test duration (maximize for speed = OFF)
> --------------------------------------------------------
> __fastcall       |     14.3900 seconds
> __stdcall        |     12.9700 seconds
> __cdecl (**)  |     13.6200 seconds
> __inline          |      12.9600 seconds
> 
> --------------------------------------------------------
> qualifier          |  test duration (maximize for speed = ON)
> --------------------------------------------------------
> __fastcall        |      5.9400 seconds
> __stdcall         |       6.5300 seconds
> __cdecl (**)   |       6.4800 seconds
> __inline           |       0.0000 seconds (*)
> 
> (*) suspicious

Actually, this is not suspicious - if the function is inlined, the
compiler can optimise the loop away. This is a common flaw with
benchmark code - not as smart as the compiler is :-)

> (**) __cdecl is the default qualifier
> 
> 
> 
> Conclusion:
> 
> __inline and __fastcall give the best results
> when the compiler is instructed to generate code
> with the flag "optimize for speed" enabled.
> 
> I also noticed that parameters marshalling is not an issue
> because the compiler will link the appropriate static library according
> to the optimization selected. Therefore, in win32, we should
> be able to optimize our C code for speed  without problems.
> 
> Enrico
> 
> 


-- 
http://www.apache-ssl.org/ben.html       http://www.thebunker.net/
**  ApacheCon - Dec 10-14th - San Diego - http://apachecon.com/ **
"There is no limit to what a man can do or how far he can go if he
doesn't mind who gets the credit." - Robert Woodruff