Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
"""
(anaconda3-4.2.0) ~/tmp% python numba_example.py [18:00:09]
Python: 3.641503095626831
Numba: 0.09061694145202637
"""
import time
from numba import jit, int32
def fib(n):
if n <= 1:
return n
else:
return fib(n - 1) + fib(n - 2)
@jit(int32(int32), nopython=True)
def fib_numba(n):
if n <= 1:
return n
else:
return fib_numba(n - 1) + fib_numba(n - 2)
def measure(f):
t = time.time()
f()
return time.time() - t
N = 35
print('Python:', measure(lambda: fib(N)))
print('Numba: ', measure(lambda: fib_numba(N)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment