From: Reid Thompson Date: 2009-02-11T13:43:04+09:00 Subject: Re: Ruby vs Perl performance William James wrote: > Reid Thompson wrote: > >> Reid Thompson wrote: >>> Ruby Elapsed 0.045477 >>> >>> >> #!/usr/local/bin/ruby >> >> require 'rubygems' >> require 'inline' >> >> BAILOUT = 16 >> MAX_ITERATIONS = 1000 >> >> class Mandelbrot >> >> def initialize >> puts "Rendering" >> for y in -39...39 do >> puts >> for x in -39...39 do >> i = iterate(x/40.0,y/40.0) >> if (i == 0) >> print "*" >> else >> print " " >> end >> end >> end >> end >> >> inline do |builder| >> builder.c " >> 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; >> } >> } >> }" >> end > >> end >> >> >> time = Time.now >> Mandelbrot.new >> puts >> puts "Ruby Elapsed %f" % (Time.now - time) > > > 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 time calc code pulled from the web... rthompso@raker ~ $ cat mandInC.c #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; struct timeval start, stop, echodelay; // start, stop and echo delay times if((gettimeofday(&start, NULL)) == -1) { perror("gettimeofday"); exit(1); } for (y = -39; y <= 39; ++y) { printf("\n"); for (x = -39; x <= 39; ++x) { i = iterate(x/40.0, y/40.0); 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; }