Skip to content

Instantly share code, notes, and snippets.

@yyuu
Last active December 19, 2015 11:49
Show Gist options
  • Save yyuu/5950016 to your computer and use it in GitHub Desktop.
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)
*.pyc
*.c
*.so
/falcon
#!/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)))
.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 $@
## 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
#!/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