Last active
May 28, 2019 19:43
-
-
Save deeplook/f565a9618dd7bfe498cb1cd0aa7cec09 to your computer and use it in GitHub Desktop.
Benchmark using Fibonacci numbers with Python, Cython and PyPy.
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 bash | |
echo "Benchmark for Fibonacci numbers with Python3, Cython and PyPy" | |
echo ' | |
def fib(n): | |
"Return the n-th Fibonacci number." | |
i = 0 | |
a, b = 0, 1 | |
if n < 2: | |
return [a, b][n] | |
while i < n: | |
a, b = b, a + b | |
i += 1 | |
return a | |
' > pyfib.py | |
set -o xtrace | |
cat pyfib.py | |
cp pyfib.py cyfib.pyx | |
NUM=100000 | |
python --version | |
python -m timeit -s "import pyfib" "pyfib.fib($NUM)" | |
python -c "import cython; print('Cython: %s' % cython.__version__)" | |
python -m timeit -s "import pyximport; pyximport.install(); import cyfib; import warnings; warnings.filterwarnings('ignore')" "cyfib.fib($NUM)" | |
pypy --version | |
pypy -m timeit -s "import pyfib" "pyfib.fib($NUM)" | |
set +o xtrace |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sweet pyx oneliner, but the warning part seems moot, like the speedup without some annotations in your pyx.