I made a great experience with Cython
(Another thing than CPython
…). You can make plain C
code out of your program, compile it to an extension and just run it from another Python file. See this question from me for more information on building an extension including numpy
: How to create a .pyd file?.
Sample:
Assuming you have your code compiled to an extension (.pyd
on Windows, .so
on every other platform), e.g. myext.pyd
and a function myfunc()
in it, you can run it like this:
import myext
myext.myfunc()
The Python Interpreter will run this little code snippet, load the extension (which is bytecode made from C
-code, will be fast!) and run any function much faster than normal Python code.
You will need to install a compiler on Windows, gcc
is already included in most Linux/Unix systems. There’s a special Compiler package for Python 2.7, but this dies not work for Python 3.x. You can use MinGW
on Windows, a Port of the gcc
which works fine for me.
Sometimes Cython
will generate 15k lines C code from about 300 lines Python Code, don’t wonder about that 🙂
Feel free to ask if you have some more questions or need a more detailed description how to do that. Building a working Python extension is not that trivial…
Hope this helps!
1
solved Python performance [closed]