PyPy is ruddy fast. So is Hy.
Time of fib(9), using a simple recursive implementation.
cPython 2.6 | cPython 2.7 | PyPy | cPython 3.2 | |
---|---|---|---|---|
Native | 11.81 seconds | 9.12 seconds | 1.06 seconds | 11.74 seconds |
Hython | 16.92 seconds | 11.63 seconds | 2.72 seconds | 11.02 seconds |
Clojure-py | 302.52 seconds | 242.64 seconds | 11.49 seconds | (can't run) |
The following tests some iteration over the following functions:
Hython (hython.hy
):
(defn fibhy [x]
(if (<= x 2)
x
(+ (fibhy (- x 2)) (fibhy (- x 1)))))
Clojure-py (cloj.clj
):
(ns cloj)
(defn fibclj [x]
(if (<= x 2)
x
(+ (fibclj (- x 2)) (fibclj (- x 1)))))
Native (builtin
)
def nfib(x):
if x <= 2:
return x
return nfib(x - 2) + nfib(x - 1)
And, the foo.py
script:
#!/usr/bin/env python
import hy.lang.importer
from hython import fibhy
import clojure.main
from cloj import fibclj
if __name__ == '__main__':
import sys
import timeit
print "Testing fib of 9, bunch of times."
print ""
print "Native impl", timeit.timeit("nfib(9)", setup="from __main__ import nfib")
print "Hython impl", timeit.timeit("fibhy(9)", setup="from __main__ import fibhy")
print "Clojure-py impl", timeit.timeit("fibclj(9)", setup="from __main__ import fibclj")
Version:
Python 2.7.2 (1.9+dfsg-2, Jul 13 2012, 03:57:27)
[PyPy 1.9.0 with GCC 4.7.1]
Output:
Testing fib of 9, bunch of times.
Native impl 1.06238484383
Hython impl 2.72712397575
Clojure-py impl 11.4938201904
Version:
Python 2.7.3
Output:
Testing fib of 9, bunch of times.
Native impl 9.12584590912
Hython impl 11.6347429752
Clojure-py impl 242.644510984
Version:
Python 2.6.8
Output:
Testing fib of 9, bunch of times.
Native impl 11.8195121288
Hython impl 16.9216940403
Clojure-py impl 302.52679801
Version:
Python 3.2.3
Output:
Native impl 11.746108055114746
Hython impl 11.024507999420166
For reference: Native C version takes just 0.105 seconds. Well, or 0.3 seconds if you include compiling:
nfib.c:
foo.c:
Benchmarked like this:
time ( gcc -O3 -Wall -Werror nfib.c foo.c -o nfib && ./nfib )