[SciPy-dev] Inclusion of cython code in scipy
Brian Granger
ellisonbg.net@gmail....
Thu Apr 24 12:28:17 CDT 2008
On Thu, Apr 24, 2008 at 10:38 AM, Prabhu Ramachandran
<prabhu@aero.iitb.ac.in> wrote:
> Brian Granger wrote:
> >> > C++ STL containers are truly great for things like this. I would
> >> > write a simple c++ header file that defines the Particle class, create
> >> > an std::vector<Particle> to hold them and wrap the whole thing into
> >> > Cython. I have already done stuff like this (templated c++ with STL)
> >> > with cython and it works great. Furthermore, you end up with an
> >> > actual C/Python extension type that 1) is super fast 2) has its own C
> >> > api that can be called from other C extensions.
>
> For some strange reason I still haven't gotten this email but Brian,
> that is the point I was making, STL has all the needed container classes
> for this and this is precisely what I use. I just use SWIG to wrap this
> C++ code and it works great. Much of the STL is already wrapped via
> SWIG and with numpy.i it interfaces quite well with numpy, in addition
> you can inject doxygen generated documentation almost automatically so
> your generated wrapper code is fully documented.
>
> I've also explored using D and PyD and it makes for a fantastic
> combination. D seems to be (gdc) about 1.7 times slower than C++ but is
> really nice to work with. PyD's support for numpy is lacking but could
> be fixed.
>
> The point is (and what I understand of what Nathan was saying), pyrex
> and cython let you wrap the code to an extent but do not provide the
> underlying solution. People were arging that Cython may be used in
> place of C, and I think our point is that it isn't there yet and for
> people used to C or C++ it is much easier to just use those instead.
I do think that in many cases, Cython _can_ be used quite well instead
of C/C++. But, for the example you gave, the C++ STL is a great tool
and probably the tool I would use if I really needed the performance.
But, for someone that doesn't know or want to deal with C/C++, Cython
does provide a nice way of writing the hypothetical Particle class as
a C/Python extension type. Here is the Cython code:
cdef class Particle:
double public x, y, z
double public px, py, pz
double public mass, charge
The resulting extension type would be nearly as fast as the
corresponding c++ class (I will test this), and is already wrapped
into Python. Of course, the user would need to store these objects in
a Python list rather than a c++ std::vector, but depending on what
they are doing, it could be totally fine. For a user who would
otherwise write this class in pure Python, Cython represents a
_massive_ improvement. One cool possibility would be to make an
stl::vector of these python extension types, which I think Cython
would allow.
>
> > Unfortunately, the context I have done this in is proprietary code for
> > my employer. But, I will try to put together a simple example this
> > weekend and post it to launchpad. The documentation on these
> > capabilities of Cython isn't great right now for sure, so some example
> > code would sure help.
>
> So why don't we do this together. I'll put together my tiny example of
> a shootout with Python C++ and D and lets see how they stack up. If you
> would be so kind you can add the cython/pyrex parts to this. Thanks.
>
> cheers,
> prabhu
>
>
> _______________________________________________
> Scipy-dev mailing list
> Scipy-dev@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-dev
>
More information about the Scipy-dev
mailing list