Very slow Numpy buffer pointer access

Viewed 847

I'm trying to get the pointer to a Numpy array so that I can manipulate it quickly in my Cython code. I found two ways of getting the buffer's pointer, one using array.__array_interface__['data'][0] and the other with array.ctypes.data. They are both painfully slow.

I have created a small Cython class that simply creates a numpy array and stores the pointer to its buffer:

cdef class ArrayHolder:
    cdef array
    cdef long *ptr

    def __init__(ArrayHolder self, allocate=True):
        self.array = np.zeros((4, 12,), dtype=np.int)
        cdef long ptr = self.array.__array_interface__['data'][0]
        self.ptr = <long *>ptr

Then, back in Python, I create multiple instances of this class, like so:

for i in range(1000000):
    holder = ArrayHolder()

This takes around 3.6 seconds. Using array.ctypes.data is half a second slower .

When I comment out the call to __array_instance__['data'] and run the code again, it completes in around 1 second.

Why is obtaining the address of the Numpy array buffer so slow?

2 Answers
Related