Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Also, add a couple of type annotations to the program:

  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")
The run:

  ~> mypyc fib.py
And boom:

  ~> python
  >>> import fib
  332.64994621276855 ms
(FYI, mypyc is a compiler that's part of the mypy package).


Wouldn't just straight running this through cython make more sense? Or numba?


Here are some timings using three different approaches using Cython, and also numba: https://share.cocalc.com/share/df81e09e5b8f16f28b3a2e818dcdd...

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).


Well, a point in mypyc’s favor is that the above code is still syntactically valid Python code.


Both numba and cpython work with python code AFAIK.

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.


there's a backslash in the print statement that crashes it




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: