[Tutor] Cython vs Python-C API
Steven D'Aprano
steve at pearwood.info
Tue Nov 15 00:18:25 CET 2011
Jaidev Deshpande wrote:
> 1. I can either compile the C code into a module using the Python-C/C++
> API, through which I can simple 'import' the required function.
> 2. I can use the Python code and extend it using Cython.
>
> Which will give me a better performance?
The only way to be sure is to do both, measure the speed of each, and
compare. But in *principle* hand-written C may be faster, but in
*practice* Cython is likely to be as fast and possibly faster. It's
certainly easier: you can afford to try Cython and only bother with the
pure C code if you actually need it.
A more interesting question is "which will give you sufficiently fast
performance for the least development time?"
Some further options:
3. Just write it in pure Python, and don't worry about speeding it up
unless you actually need to. Unless you have actually *measured* the
time taken, how do you know which (if any) bits are too slow? Perhaps it
is already fast enough, and making it faster just throws away developer
time for no benefit.
If the cubic spline interpolation is too slow, you still have additional
options:
4. If you only need to support Python 2 on 32-bit platforms, use the
Psyco JIT compiler to speed it up. This only works on the standard
CPython version (not to be confused with Cython), not IronPython or
Jython compilers.
5. Use PyPy, which is an optimizing compiler for Python. This is likely
to be ten times as fast as CPython. PyPy is fast enough to do some video
processing tasks in real-time, so it likely will be fast enough for
cubic splines.
http://morepypy.blogspot.com/2011/07/realtime-image-processing-in-python.html
6. Extend the Python code with Cython, and see if that is sufficiently
fast enough for your needs.
7. And finally, if the Cython code is too slow, re-write in C by hand.
--
Steven
More information about the Tutor
mailing list