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

Daniel Fetchinson fetchinson at googlemail.com
Sat Dec 27 23:27:53 CET 2008


>> I'm trying to write an extension module in C which contains a single
>> function with the following prototype:
>>     void func( int N, int * arg1, int * arg2, int * ret );
>> Here arg1 and arg2 are length N arrays, and the function computes ret
>> which is also an N length array. From python I'd like to call this
>> function as
>> ret = func( [ 1, 2, 3 ], [ 2, 3, 4] )
>
> This requirement pretty much dictates the slow answer you have.

I'm not sure I understand what you mean.

>  > Does this mean that I can only pass the arrays from python to C as
>  > generic python objects and in a later operation I need to get the
>  > elements from this generic python object, construct a C array and pass
>  > that to the C function? ... What's the simplest way of doing this?
>
> Either use ctypes, look into array.array, or look into numpy.array.

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).

> I'd just use numpy, myself:
>      import numpy
>      total = numpy.array([1, 2, 3]) + numpy.array([2, 3, 4])

What makes you think I want to add two arrays?

Cheers,
Daniel


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



More information about the Python-list mailing list