[pypy-issue] [issue1526] Faster cffi/numpypy array data pointer casting (ndarray.ctypes?)

mikefc tracker at bugs.pypy.org
Thu Jul 4 01:59:23 CEST 2013

New submission from mikefc <coolbutuseless at gmail.com>:

When calling out to functions in C (using cffi) to operate on numpypy arrays, 
the current method to get a pointer is to do:

a_ptr   = ffi.cast("double *",   a.__array_interface__['data'][0])

For small functions, the overhead of this fetch-and-casting of the data address 
is quite significant.

I've included some code that compares a simple dot product of a vector (length = 
4), using numpypy's dot function, a python implementation, and 2 c 
implementations called via CFFI.  One does the cast every time the function is 
called, and one does the cast outside the timing loop to demonstrate how much 
time the lookup-and-cast is taking:

The results are:

                     numpypy dot 0.797430430847 0:00:03.590898
                      python dot 0.797430430847 0:00:01.130100
 c dotproduct (cast to double *) 0.797430430847 0:00:02.245106
       c dotproduct (no casting) 0.797430430847 0:00:00.352964

* Is there currently a faster way to do this cffi cast and function call?
* Might implementing ndarray.ctypes in numpypy make this faster?

files: numpypy_cffi.zip
messages: 5899
nosy: mikefc, pypy-issue
priority: wish
status: unread
title: Faster cffi/numpypy array data pointer casting (ndarray.ctypes?)

PyPy bug tracker <tracker at bugs.pypy.org>

More information about the pypy-issue mailing list