Skip to content


robertwb edited this page · 3 revisions
Clone this wiki locally

Views of the Sage project are expressed here.

A nice comparison of Python Cython, Swig, and PyD by Prabhu Ramachandran

Lisandro Dalcin wrote the following in a response to a question about SWIG

I saw a mail from you, forwarded by W. Stein to Cython-Devel list, about you are trying to measure performance for SWIG vs. Cython/Pyrex.

Well, I'm a rather power SWIG user, and now, after about a month I believe I'm a rather good Cython user.

My main insterest is parallel distributed computing with Python. From some time, I've been developing two very important (at least for me!) packages, mpi4py and petsc4py (just google for the link).

For your objectives, I believe petsc4py could be interesting. The C API of PETSc is wrapped with SWIG, but then this stuff is used inside a Python code to implement a fully OO, pythonic API. I've heavily hacked in the way SWIG implements its infamous 'this'. I do not rely on this mechanism for passing object between Python and C layers, instead, I've implemented a full base type object in C; and all this crap is because the normal SWIG way was not enough for me.

But now, after fall in love with Cython, I've started to port (in fact, write from scratch) all my work to use Cython. The petsc4py project is not yet ready for the public, but it is in good shape for testing.

And now, the interesting part. I've wrote some testing for my thesis presentation (it will be in about a month), where I've crafted some numerical tests in order to stress the overhead of passing back a forth objects from the C layer to the Python layer and viceversa. All the actual numericall computing is done in C or Fortran 90. And all runs are sequential, not parallel.

This testing is done, code and results available, but only for the former SWIG based version of petsc4py. The whole problem is related to use Krylov iterative methods for solving a linear system of equations arising from a finite-diferences discretization of the 3D Poisson problem. BUT note that I never build a sparse matrix, instead I use a 'matrix-free' version with implements the matrix-vector product A*x -> y as a function F(x) -> y, thus being crude computing with Fortran 90 arrays.

If you are interested in take a look at this, I can send you some PDF pages describing all this testing. I can also send you the code implementing this testing. And if you want to go further, I perhaps can find some spare time to help you implement all this testing with the new Cython-based version of petsc4py that I'm writting.

I expect that my SWIG implementation will run faster that a normal SWIG wrapper (as I've hacked on many ways to ge it faster). And I expect that the new Cython based version is still faster.

I really believe that this is a very good test case for comparing SWIG vs. Cython, as all the numerical computing is actully done in C, then you finally measure the overhead of passing object around.

Lisandro Dalcín

Something went wrong with that request. Please try again.