People are missing the point with the Python code. The C++ code and the Lisp code aren't particularly optimized, either. The point of the benchmark is to compare the relative speeds of roughly the same Fibonacci code out of the box.<p>On SBCL, a memoized recursive fibonacci is about twice as fast as the Lisp code given also running in SBCL on my machine, for example.<p>I'm more suspicious about why the C++ code is so slow.<p>Edit: I wrote my own C++ Fib code and tried benchmarking it outside of Clasp (<a href="https://gist.github.com/jl2/4d74958b02b3caea2f5c" rel="nofollow">https://gist.github.com/jl2/4d74958b02b3caea2f5c</a>). It routinely ran in less than 0.005 seconds, which seemed too fast, so I looked at the assembly output. AFAICT, the compiler is smart enough to realize fib() has no side effects and is "pure", and is computing the value at compile time, reducing the function call to essentially be myfib = 8944394323791464. It almost seems like an unfair comparison, but since it's comparing compiler performance, I think it's relevant information.
I was a bit curious, so I did a short experiment:<p><a href="http://nbviewer.ipython.org/urls/dl.dropbox.com/s/l9naqibqytv8vjt/Numba_Fib.ipynb" rel="nofollow">http://nbviewer.ipython.org/urls/dl.dropbox.com/s/l9naqibqyt...</a><p>Using numba, and adding a one line decorator to the function (without any changes whatsoever) we get around 2 orders of magnitude speed up.<p>Note - this doesn't involve any fancy re-writing, annotating, etc, you literally just add a decorator.<p>Writing really fast numerical code in Python is very easy. There's absolutely no reason not to use numba if you have small functions that just do number crunching. numba can even inline other numba functions - so you don't even pay the function call overhead.
In Python 2.7, shouldn't you be using xrange() rather than range()? xrange() is a generator whereas range() will actually create the entire list and iterate it.<p>In case anyone isn't aware: in Python 3, range()'s implementation was effectively replaced with that of xrange().