3 ms·
I used this a long time ago, and from my observations, the biggest gains occurred in loops. (They are the only time Python is really slow anyway.) However, the
by baltcode 16y ago
I used this a long time ago, and from my observations, the biggest gains occurred in loops. (They are the only time Python is really slow anyway.) However, the last time I checked interfacing with numpy and scipy was not supported.
- cdavid 16y agoThen you checked a very long time ago :) It is quite easy to pass numpy arrays to cython functions. I would say that except in some very special cases, writing raw Python C interface is useless (of course, writing numerical code which is independent of the C API is still very useful). Cython also gives you the benefit of py3x compatibility (cython generates wrappers which can be compiled for both python 2 and python 3).
- rdtsc 16y ago> I would say that except in some very special cases, writing raw Python C interface is useless ... I would disagree. Cython is less known and has a learning curve. CPython extension interface is well established and more common. That is not an insignificant advantage. Do you stop for a couple weeks to learn Cython, and do you have complete confidence in its generated code, or do you just start using something you know and is tried and true? It depends. We chose C Python extensions or just writing hotspots in C in a separate process.
- cdavid 16y agoThe C API also has a learning curve, and is certainly harder than cython. You don't need a couple of weeks to learn cython - I rewrote one small package from ctypes to cython in ~ 1 day, without previous experience in cython. The problem of C python extensions is that it is so hard to write correctly because of reference counting.
- another 16y agoI'm not sure of your needs, but there's now reasonable support for efficient interaction with numpy arrays; see, eg http://docs.cython.org/src/tutorial/numpy.html http://docs.cython.org/src/tutorial/numpy.html and/or http://conference.scipy.org/proceedings/SciPy2009/paper_2/full_text.pdf http://conference.scipy.org/proceedings/SciPy2009/paper_2/fu... Some types of array addressing will take the slower---but still correct---invocation path through Python, but the common form is compiled to direct (albeit strided) array access.
- baltcode 16y agoThanks, that looks great! I used to do stuff like that with weave. It would be interesting to see the speed comparison for weave and cython.