Archive for May, 2022
Compile Python module to C extension
May 1, 2022
Leave a comment
I found an interesting blog post: You Should Compile Your Python And Here’s Why. In short: if you have a type-annotated Python code (mypy), then with the command mypyc
you can compile it to a C extension. Under Linux, the result will be an .so file, which is a binary format.
Here you can find a concrete example: https://github.com/jabbalaci/SpeedTests/tree/master/python3
Steps:
- We need a type-annotated source code (example). It’s enough to add type hints to functions. It’s not necessary to annotate every single variable.
- Compile it (
mypyc main.py
). The result is an .so file. - You can import the .so file as if it were a normal Python module.
- If your project consists of just one file (
main.py
) that you compiled, here is a simple launcher for it:
#!/usr/bin/env bash
python3 -c "import main; main.main()"
If you do a lot of computation in a function, then with this trick you can make it 4-5 times faster.
I don’t think I would use it very often, but it’s good to know that this thing exists. And all you have to do is to add type hints to your code.
Links