Hacker News new | past | comments | ask | show | jobs | submit login

I wish that you could use typed Python to speed up the execution of the program instead of them just being hints.



But that is precisely what mypyc[0] is doing!

It is part of mypy, and it compiles type-valid python files to cpython binary extensions, and takes advantage of a lot of shortcuts available to cut execution time. It is still a bit early to advocate for its use everywhere, and has it drawbacks (extensions compiled with mypyc will also typecheck the inputs and outputs of the module at runtime, which is great for improving code quality and validity of the annotations, but also means you may get TypeErrors in production if you hit a bad edge case).

If there is no shortcut it will fallback to normal, slow, python performance, with some smallish benefits (e.g. vtables for attribute lookups, no parsing time, etc).

[0] https://mypyc.readthedocs.io/


That's really neat, I hadn't heard of mypyc!

How does it compare to Nuitka? https://nuitka.net


I have not ran benchmarks, I would expect Nuitka to be more mature and optimized, but I really like the idea of having it bound with the type-checker.


Not exactly what you're talking about, but... For some types of functions, Numba's JIT offers some pretty nice speed ups. It can infer function types (or you supply them yourself) and compiles to optimized machine code


It's tricky, since only the typed part can be sped up, and there will usually be lots of back-and-forth between typed and untyped code; which requires conversions and checks.

For comparison, Racket has gradual typing which gives a speed boost when everything is typed, but can end up much slower if anything remains untyped, e.g. see figure 3 in http://www.ccis.northeastern.edu/home/types/publications/gra...

Python would probably be even worse, since it's "more dynamic" than Racket; e.g. there are many hooks that can run arbitrary code (like dunder methods), and there's little encapsulation so values may get swapped out via monkey-patching at any time.


Nim feels very close to a typed Python.


afaik that was first planned for Python 4 and then dropped again :(


Any links on it being planned for Python 4? I didn't know they'd ever even considered it.


I think that the mentions of that have been deleted from the relevant PEP since, but e.g. https://www.reddit.com/r/Python/comments/7zvyhx/pep_563_ment... is also asking about it.




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

Search: