I have recently started to use ctypes to run some C code within python. To
pass an array to C code, if self is a numpy array, I found that a popular
solution is the one to use
However, once profiling the python script, I realized that a lot of time
was being wasted in these three functions:
The solution is not completely clear to me. I realized that, if a numpy
array needs to be fed to a C function multiple times, it pays off to create
a C array (with something like "(ctypes.c_double * n)()"), copy the
elements from the numpy array in the C array, and feed this array to the C
function. I am not sure why this is the case, as extracting a pointer from
a numpy array should be fairly straightforward. But I guess when invoking
the self.ctypes command, a ctypes object is constructed and this has a
Is there something else that can be done? Overall, I find this misleading,
as this seems a popular way to use ctypes, while most people trying to pass
arrays to C functions do so to achieve maximum efficiency with the C code.
I am really not an expert, so please let me know if I am missing something.