~> python fib.py 4825.7598876953125 ms ~> pypy3 fib.py 514.7459506988525 ms
@numba.jit def fib(n): if n == 1 or n == 0: return 1 return fib(n - 1) + fib(n - 2)
498.46601486206055 ms.
second run:
89.19310569763184 ms.
For completeness, with njit:
@numba.njit def fib(n): if n == 1 or n == 0: return 1 return fib(n - 1) + fib(n - 2)
152.62889862060547 ms
86.35592460632324 ms
from numba import jit @jit def fib(n: int) -> int:
import time def fib(n: int) -> int: if n == 1 or n == 0: return 1 return fib(n - 1) + fib(n - 2) t0 = time.time() fib(35) t1 = time.time() print(f"{(t1 - t0) \* 1000} ms")
~> mypyc fib.py
~> python >>> import fib 332.64994621276855 ms
Numba wins and is about twice as fast as my most clever Cython code. Naive Cython is pretty bad, but more clever Cython is reasonably good (though not as good as numba).
I don't know that cpython would take advantage of mypy annotations and you can make a cpython program not python-compatible, but you don't have to.