|
From: Ioannis V. <no...@ho...> - 2002-08-16 14:06:49
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of mo...@ju...
> Sent: Friday, August 16, 2002 8:02 AM
> To: dev...@li...
> Subject: [Dev-C++] speed test
>
>
> Running the program below in Dev-C++, Version 4, took between
> 209 and 235
> seconds on my system. I don't know why the running times
> vary, but even
> taking the fastest time of 209 seconds, it compares unfavorably with a
> similar program compiled (36 seconds) or interpreted (55 seconds) in
> QBasic 4 .
Did you printed the result after every x++ inside the for loop in QB?
>
> Does Dev-C++ really produce such leisurely code? Or can the
> program be
> speeded up?
Of course it can. Printing in console is slow (the time cost of a
function call, etc).
>
> Allan
>
> // This program prints (1) integers 1 to 1,000,000
> // (2) starting & ending times
> //======================================================
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> int main(int argc, char *argv[])
Theese arguments are not needed.
> {
> struct tm *ptr;
> time_t loc_time;
> long int x;
>
> loc_time = time(NULL);
No accurate results with this method.
> ptr = localtime(&loc_time);
>
> for (x = 1;x <= 1000000;x++)
> printf("%d ",x);
Here you have 100000 function calls for printing in the console.
>
> printf("\nStarting time is ");
> printf(asctime(ptr));
>
> loc_time = time(NULL);
> ptr = localtime(&loc_time);
> printf("\nEnding time is ");
> printf(asctime(ptr), "\n");
>
> system ("PAUSE");
>
> return 0;
> }
A better program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
clock_t t1, t2;
long int x;
t1=clock();
for (x = 1; x <= 1000000;x++)
;
printf("%ld\n", x);
t2=clock();
printf("\n\nProcessor time: %lu", (unsigned long)t2-t1);
printf("\nIn seconds: %f\n\n", (double)(t2-t1)/CLOCKS_PER_SEC);
system ("PAUSE");
return 0;
}
The above prints the final value of x only.
Using MINGW GCC 3.1 with optimizations:
gcc temp.c -o temp -ansi -pedantic-errors -Wall
-fexpensive-optimizations -O3 -ffloat-store -mcpu=pentiumpro
C:\c>temp
1000001
Processor time: 0
In seconds: 0.000000
Press any key to continue . . .
Here obviously the compiler performed an optimisation with which he saw
the final result of the loop and avoided the loop completely.
Running the GCC 3.1 without the optimisation switches:
C:\c>gcc temp.c -o temp -ansi -pedantic-errors -Wall
C:\c>temp
1000001
Processor time: 10
In seconds: 0.010000
Press any key to continue . . .
Here it performed the loop and it took it 10 msec (something similar
will happen with Dev-C++ eith optimisations off).
In summary, your time cost was imposed due to 1000000 function calls to
printf().
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.cjb.net
* Alternative URL: http://run.to/noicys
|