Perl, Python and Ruby
After reading this article comparing the new Ruby 1.9 to it’s older version and Python, I thought it would be interesting to see how my preferred language, Perl, compares. First, because my hardware is certainly different from the author of that post, I re-ran the Python and Ruby 1.9 tests:
Python: real 0m43.415s user 0m41.455s sys 0m0.132s Ruby: real 0m35.379s user 0m33.722s sys 0m0.112s
I threw together some quick Perl:
sub fib { my $n = shift; if ( $n == 0 or $n == 1 ) { return $n; } else { return fib($n - 1) + fib($n - 2); } } for (0..35) { print "n=$_ => " . fib($_) . "\n"; }
and ran it:
Perl: real 1m15.329s user 1m9.188s sys 0m0.364s
That’s impressive; Perl sucks at this recursive implementation of computing Fibonacci numbers. Since I had the makings of a little suite of benchmarks, I implemented this test in C and Java:
C code:
include
int fib(int n) { if ( (n == 0) || (n == 1) ) return n; else return (fib(n-1) + fib(n-2)); } //fib
int main() { int x; for (x = 0; x < 36; x++) { printf( "n = %d => %d\n", x, fib(x) ); } //for
1 return 0;}
C timing:
real 0m0.605s user 0m0.564s sys 0m0.000s
Java code:
class Fib { static int fib(int n) { if ( (n == 0) || (n == 1) ) return n; else return (fib(n-1) + fib(n-2)); } //fib
1 2 3 4 5 public static void main(String args[]) { for (int x = 0; x < 36; x++) { System.out.println( "n = " + x + " => " + fib(x) ); } //for } //main} //Fib
Java timing:
real 0m0.455s user 0m0.412s sys 0m0.012s
It’s very interesting to see the huge speed disparities in these languages, but remember, this was a single, very limited test; don’t take these benchmarks as having any significant meaning. Really, don’t take them to mean much of anything, unless all you do is write lots of simple recursive code.