[Numpy-discussion] NumPtr vs NumPy.i to access C

Brian Granger ellisonbg.net@gmail....
Sat May 17 21:30:06 CDT 2008


>> Cython is a different approach from SWIG (see
>> http://wiki.cython.org/WrappingCorCpp; in particular SWIG uses more layers
>> of indirection).
>>
>
> >From the link:
> "[SWIG] Can wrap almost any C and C++ code, including templates etc.
> Disadvantage is that it produces a C file, this compiles to .so, but
> then it also produces a Python wrapper on top of this .so file. So
> it's messy and it's slow. Also SWIG is not only targeting Python, but
> other languages as well."
>
> I really wish that people didn't spread FUD about SWIG.  For
> reference, here's the "messy and slow" Python wrapper for a function
> in scipy.sparse:

I have taken the liberty of making the Cython wiki a little more
specific and fair to SWIG.  Here is the changed text:

SWIG is one of the oldest and most mature methods of wrapping C or C++
code into Python (SWIG works for other target languages as well). It
can wrap almost any C and C++ code, including complex templated C++
code. If you have a large (so that hand wrapping is prohibitive) and
course grained API, SWIG will likely be your best choice. However,
SWIG does have a number or disadvantages compared with Cython. First,
SWIG produces a C file, which gets compiled to a .so, but then it also
produces a Python wrapper on top of this .so file. For fine grained
APIs (where not much is done in each C/C++ call), the overhead of this
additional Python wrapper can be significant. Second, with SWIG, the
Python wrappers are written for you, so if their design is not exactly
what you want, you end up doing more work to create your final Python
API.

Please correct any new errors I have introduced.

Cheers,

Brian


More information about the Numpy-discussion mailing list