New submission from mikefc <[email protected]>:

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


Questions:
* 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 <[email protected]>
<https://bugs.pypy.org/issue1526>
________________________________________
_______________________________________________
pypy-issue mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-issue

Reply via email to