Skip to content

Instantly share code, notes, and snippets.

@rwev
Created October 16, 2019 16:26
Show Gist options
  • Save rwev/c1af022c4279809b70226c452781ad92 to your computer and use it in GitHub Desktop.
Save rwev/c1af022c4279809b70226c452781ad92 to your computer and use it in GitHub Desktop.
Compare fibonacci calculation speed in Python and Cython
try:
from setuptools import setup
except ImportError:
from distutils.core import setup
import pyximport; pyximport.install()
import fib
from time import time
def timems (itern, func, argtup):
t = time()
for i in range(itern):
func(*argtup)
ms = 1000 * (time() - t)
return ms
FIBN = 90
ITERATIONS = 100000
def fibpy(n):
a, b = 0.0, 1.0
for i in range(n):
a, b = a + b, a
return a
pure_py_ms = timems (ITERATIONS, fibpy, (FIBN,))
print "\nPYTHON FIB(%d) = %0.0f" % (FIBN, pure_py_ms)
cython_ms = timems (ITERATIONS, fib.fibcy, (FIBN,))
print "\nCYTHON FIB(%d) = %0.0f ms" % (FIBN, cython_ms)
print "\nSPEEDUP = %0.0f\n" % (pure_py_ms / cython_ms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment