#!/usr/bin/env python # coding: utf-8 # ## Comparing Cython, mpypyc, and default Python speed # # On a simple thing: # - mypyc gives a 10x speed-up # - cython-3 a 40x speed-up # In[1]: get_ipython().run_line_magic('load_ext', 'mypyc_ipython') # In[2]: def py_fibonacci(n: int) -> int: if n <= 1+1: return 1 else: return py_fibonacci(n-1) + py_fibonacci(n-2) # In[3]: get_ipython().run_cell_magic('mypyc', '', 'def my_fibonacci(n: int) -> int:\n if n <= 2:\n return 1\n else:\n return my_fibonacci(n-1) + my_fibonacci(n-2)\n') # In[4]: get_ipython().run_line_magic('load_ext', 'cython') # In[5]: get_ipython().run_cell_magic('cython', '', 'cpdef int cy_fibonacci(int n):\n if n <= 2:\n return 1\n else:\n return cy_fibonacci(n-1) + cy_fibonacci(n-2)\n') # In[6]: get_ipython().run_line_magic('timeit', 'py_fibonacci(30)') # In[7]: get_ipython().run_line_magic('timeit', 'my_fibonacci(30)') # In[8]: get_ipython().run_line_magic('timeit', 'cy_fibonacci(30)') # In[ ]: