Returning a lot of data from a c extension (SWIG,array, Numeric)
Marcus Stojek
stojek at part-gmbh.de
Fri Jan 31 12:54:54 EST 2003
Hi,
I have a c-extension that receives looong lists of doubles,
does some calculations and then returns those in another
loong list. After some hard work (don't know much about c)
I got it running with SWIG and can use my module very
comfortable now.
//typedef double d_list_out
%typemap(argout) (d_list_out *, int) {
PyObject *o, *o2, *o3;
PyObject *item;
int i;
double *vec =$1;
int size =$2;
//this is so s...l........o...........w
o=PyList_New(0);//or better PyList_New(size)?
for (i=0;i<size;i++) {
item = PyFloat_FromDouble(vec[i]);
PyList_Append(o,item);
}
//.....................................
if ((!$result) || ($result == Py_None)) {
$result = o;
} else {
if (!PyTuple_Check($result)) {
PyObject *o2 = $result;
$result = PyTuple_New(1);
PyTuple_SET_ITEM($result,0,o2);
}
o3 = PyTuple_New(1);
PyTuple_SetItem(o3,0,o);
o2 = $result;
$result = PySequence_Concat(o2,o3);
Py_DECREF(o2);
Py_DECREF(o3);
}
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.
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.
Or is the Numeric module what i need? Again I have no idea
how to use it in this context.
Thanks for any help.
marcus
More information about the Python-list
mailing list