Last active
December 19, 2015 11:49
-
-
Save yyuu/5950016 to your computer and use it in GitHub Desktop.
performance comparison on fibonacci between CPython, Cython, PyPy and [falcon](https://github.com/rjpower/falcon)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.pyc | |
*.c | |
*.so | |
/falcon |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
def fib(n): | |
return fib(n-2) + fib(n-1) if 2 <= n else 1 | |
for n in range(1, 40): | |
print("fib(%d) = %d" % (n, fib(n))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.PHONY: all | |
.SUFFIXES: .c .py .so | |
all: fib.so | |
.py.c: | |
cython $< -o $@ | |
.c.so: | |
$(CC) -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing $(shell python-config --includes) $< -o $@ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## CPython ## | |
python fib.py | |
real 1m32.552s | |
user 1m32.388s | |
sys 0m0.024s | |
## falcon ## | |
python -m falcon fib.py | |
I 0.000 [17045] src/falcon/rcompile.cc:1751 COMPILED code, 9 registers, 17 operations, 24 stack ops. | |
I 0.000 [17045] src/falcon/rcompile.cc:1751 COMPILED fib, 7 registers, 11 operations, 18 stack ops. | |
real 1m12.279s | |
user 1m12.080s | |
sys 0m0.036s | |
## Cython ## | |
python -c import fib | |
real 0m42.643s | |
user 0m42.504s | |
sys 0m0.036s | |
## PyPy ## | |
pypy fib.py | |
real 0m9.493s | |
user 0m9.320s | |
sys 0m0.096s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
t() { | |
echo "## $1 ##" | |
shift | |
echo "$@" | |
time "$@" 1>/dev/null | |
} | |
eval "$(pyenv init -)" | |
## CPython | |
pyenv shell 2.7.5 | |
t "CPython" python fib.py | |
## falcon | |
pyenv shell 2.7.5 | |
{ ( test -d falcon || git clone https://github.com/rjpower/falcon.git ) && | |
( cd falcon && python setup.py install ) } 1>/dev/null 2>&1 | |
t "falcon" python -m falcon fib.py | |
## Cython | |
pyenv shell 2.7.5 | |
pip install "Cython==0.19.1" 1>/dev/null 2>&1 | |
make 1>/dev/null 2>&1 | |
t "Cython" python -c "import fib" | |
## PyPy | |
pyenv shell pypy-2.0.2 | |
t "PyPy" pypy fib.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment