From: Reid Thompson Date: 2009-02-12T22:05:43+09:00 Subject: Re: Ruby vs Perl performance William James wrote: > Reid Thompson wrote: > >> William James wrote: > >>> Using the functional language F#: >>> >>> 0.0400575999999999 on my laptop with 2GHz Pentium. >>> >> On same box using all C. >> Echo delay is 0s and 20842us -> 20842 microseconds = 0.020842 >> seconds. This is an Intel(R) Core(TM)2 CPU 6320 @ 1.86GHz box w 2GB >> RAM running gentoo with entire system compiled with >> CFLAGS="-march=prescott -O2 -g -pipe" + feature splitdebug ( >> everything compiled with debug except mandInC ). -rwxr-xr-x 1 >> rthompso staff 7104 2009-02-10 23:36 mandInC > > To make timing more accurate, let's calculate the set 100 times > but draw it only 1 time. > > Result: 1.99286559999999 (laptop with 2GHz Pentium M) Echo delay is 2s and 23737us #include #include #include /** * this function is for computing the time difference between timeval x and y * the result is stored in result */ int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) { /* Perform the carry for the later subtraction by updating y. */ if (x->tv_usec < y->tv_usec) { int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; y->tv_usec -= 1000000 * nsec; y->tv_sec += nsec; } if (x->tv_usec - y->tv_usec > 1000000) { int nsec = (x->tv_usec - y->tv_usec) / 1000000; y->tv_usec += 1000000 * nsec; y->tv_sec -= nsec; } /* Compute the time remaining to wait. tv_usec is certainly positive. */ result->tv_sec = x->tv_sec - y->tv_sec; result->tv_usec = x->tv_usec - y->tv_usec; /* Return 1 if result is negative. */ return x->tv_sec < y->tv_sec; } int iterate (double x, double y) { int BAILOUT = 16; int MAX_ITERATIONS = 1000; double cr = y-0.5; double ci = x; double zi = 0.0; double zr = 0.0; double zr2 = 0.0; double zi2 = 0.0; int i = 0; double temp = 0.0; while (1) { i += 1; temp = zr * zi; zr2 = zr * zr; zi2 = zi * zi; zr = zr2 - zi2 + cr; zi = temp + temp + ci; if ( zi2 + zr2 > BAILOUT) { return i; } if ( i > MAX_ITERATIONS) { return 0; } } } int main() { int y = -39; int x = -39; int i = -1; int loop = 1; struct timeval start, stop, echodelay; // start, stop and echo delay times if((gettimeofday(&start, NULL)) == -1) { perror("gettimeofday"); exit(1); } for (loop == 1; loop <= 100; ++loop) { for (y = -39; y <= 39; ++y) { if (loop == 1){printf("\n");} for (x = -39; x <= 39; ++x) { i = iterate(x/40.0, y/40.0); if (loop == 1) { if (i == 0) printf("*"); else printf(" "); } } } } if((gettimeofday(&stop, NULL)) == -1) { perror("gettimeofday"); exit(1); } /* compute time delay */ timeval_subtract(&echodelay, &stop, &start); printf("\nEcho delay is %ds and %dus\n", echodelay.tv_sec, echodelay.tv_usec); return 0; }