C API: array of floats/ints from python to C and back

Daniel Fetchinson fetchinson at googlemail.com
Sun Dec 28 00:50:34 CET 2008

>> I have considered using ctypes but for my needs using the C API
>> directly seems more reasonable. array.array and numpy.array doesn't
>> fit my needs since I need to do long and complicated operations on the
>> two (pretty large) integer arrays that would be too slow using
>> array.array and numpy.array (I've verified this claim by benchmarking
>> a numpy.array based solution).
> OK, but if you go to array.array or numpy.array or ctypes, you can
> create a uniformly typed (C datatype) array , which is the key to
> getting any speed out of the deal.  If you insist on using python
> lists, you are stuck with extracting data element by element from
> its Python language wrap.

I thought that going through the python C API will be more
cross-platform than using ctypes, but I might be wrong. Anyway, the C
API for such a simple thing as a single function doesn't seem too
scary :)

I agree that array.array is more efficient than a list but the input
for my function will come from PIL and PIL returns a list. So I have a
list to begin with which will be passed to the C function. I assume
converting the list to an array.array and passing that to the C
function doesn't make any difference in terms of speed since the
operation itself will be done in the C function anyway.


Psss, psss, put it down! - http://www.cafepress.com/putitdown

More information about the Python-list mailing list