From: George Ogata Date: 2004-08-28T03:55:33+09:00 Subject: Re: performance comparison Boris Glawe writes: > What's the reason for this big performance difference ? I don't know what your C code looks like, but when you consider what must go in a typical C statement vs. an equivalent ruby statement, I don't find it too surprising. How do other interpreted languages (e.g., python) go? As a simpler example, observe a dumb-Fibonacci: ----- sources ---- g@crash:~/tmp$ #### C version #### g@crash:~/tmp$ cat test.c #include int fib(int n) { switch (n) { case 0: return 0; case 1: return 1; default: return fib(n-1) + fib(n-2); } } int main(int argc, char **argv) { int n; sscanf(argv[1], "%d", &n); printf("%d\n", fib(n)); return 0; } g@crash:~/tmp$ #### ruby version #### g@crash:~/tmp$ cat test.rb def fib n if n > 1 fib(n-1) + fib(n-2) elsif n.zero? 0 else 1 end end puts fib(ARGV[0].to_i) g@crash:~/tmp$ #### python version #### g@crash:~/tmp$ cat test.py import sys def fib(n): if n > 1: return fib(n-1) + fib(n-2) elif n == 0: return 0 else: return 1 print fib(int(sys.argv[1])) ----- runs ----- g@crash:~/tmp$ gcc test.c && /usr/bin/time ./a.out 35 9227465 0.44user 0.00system 0:00.44elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (100major+13minor)pagefaults 0swaps g@crash:~/tmp$ /usr/bin/time ruby test.rb 35 9227465 22.95user 0.07system 0:23.02elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (243major+168minor)pagefaults 0swaps g@crash:~/tmp$ /usr/bin/time python test.py 35 9227465 17.24user 0.00system 0:17.30elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (370major+250minor)pagefaults 0swaps That's about 52x longer for ruby. Not as big a coefficient as your program, but the difference between the implementations (in terms of complexity of the expressions evaluated) in my example is probably much less than in yours due to the simplicity of the programs. Again, this is highly dependant on what your C version looks like. HTH.