Returning a lot of data from a c extension (SWIG,array, Numeric)

Chad Netzer cnetzer at mail.arc.nasa.gov
Fri Jan 31 16:23:47 EST 2003


On Fri, 2003-01-31 at 09:54, Marcus Stojek wrote:
> Hi,
> 
> I have a c-extension that receives looong lists of doubles,
> does some calculations and then returns those in another 
> loong list.

> But what a shock! Returning my c-array into a Python list
> takes almost as long as the calculations would have if done
> in Python.

The python list data structure is not (particularly) suited to storing
large ordered sequences of homogeneous types (ie. huge arrays of floats
and doubles)

> deja gave me a hint to use the array module to transform
> 'raw memory' into a list. But I have no idea how to this.

The array structure is light on operations, but will more efficiently
store the doubles.  Furthermore, you can pass the raw data off to C
functions by handing over a pointer (rather than a new copy of the
data), and then get it back. (with some care)

> Or is the Numeric module what i need? Again I have no idea
> how to use it in this context.

If you don't mind installing Numeric (not much work at all), this is
feasible.  There are many examples on the web of how to access Numeric
data structures in your own C code.  It is more 'heavyweight' than the
array module structures, and some care muct be taken with both.  But
probably about the same amount of code and work as dealing with Python
lists in C. 

The main point is that if you use Numeric, you can do a LOT of
mathematical operations from Python itself, and may not even need to
write your extension in C (and installing Numeric is generally easier
than having to maintain a C extension to Python).

You can search the Numeric mailing list archives for examples and help. 
(I have never had to use the Numeric interface in C, so I can't help you
there, I'm afraid)

-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer
(any opinion expressed is my own and not NASA's or my employer's)







More information about the Python-list mailing list