Skip to content

Instantly share code, notes, and snippets.

@CTimmerman
Forked from deeplook/benchmark_fib.sh
Created May 28, 2019 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CTimmerman/0fbe7d688ab0f5a906ef3ba23462a12b to your computer and use it in GitHub Desktop.
Save CTimmerman/0fbe7d688ab0f5a906ef3ba23462a12b to your computer and use it in GitHub Desktop.
Benchmark using Fibonacci numbers with Python, Cython and PyPy.
#!/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