Note that there are some explanatory texts on larger screens.

plurals
  1. POcython memoryview slower than expected
    primarykey
    data
    text
    <p>I've started using memoryviews in cython to access numpy arrays. One of the various advantages they have is that they are considerably faster than the old numpy buffer support: <a href="http://docs.cython.org/src/userguide/memoryviews.html#comparison-to-the-old-buffer-support" rel="nofollow">http://docs.cython.org/src/userguide/memoryviews.html#comparison-to-the-old-buffer-support</a></p> <p>However, I have an example where the old numpy buffer support is faster than memoryviews! How can this be?! I wonder if I'm using memoryviews correctly?</p> <p>This is my test:</p> <pre><code>import numpy as np cimport numpy as np cimport cython @cython.boundscheck(False) @cython.wraparound(False) cpdef np.ndarray[np.uint8_t, ndim=2] image_box1(np.ndarray[np.uint8_t, ndim=2] im, np.ndarray[np.float64_t, ndim=1] pd, int box_half_size): cdef unsigned int p0 = &lt;int&gt;(pd[0] + 0.5) cdef unsigned int p1 = &lt;int&gt;(pd[1] + 0.5) cdef unsigned int top = p1 - box_half_size cdef unsigned int left = p0 - box_half_size cdef unsigned int bottom = p1 + box_half_size cdef unsigned int right = p0 + box_half_size cdef np.ndarray[np.uint8_t, ndim=2] box = im[top:bottom, left:right] return box @cython.boundscheck(False) @cython.wraparound(False) cpdef np.uint8_t[:, ::1] image_box2(np.uint8_t[:, ::1] im, np.float64_t[:] pd, int box_half_size): cdef unsigned int p0 = &lt;int&gt;(pd[0] + 0.5) cdef unsigned int p1 = &lt;int&gt;(pd[1] + 0.5) cdef unsigned int top = p1 - box_half_size cdef unsigned int left = p0 - box_half_size cdef unsigned int bottom = p1 + box_half_size cdef unsigned int right = p0 + box_half_size cdef np.uint8_t[:, ::1] box = im[top:bottom, left:right] return box </code></pre> <p>The timing results are:</p> <p>image_box1: typed numpy: 100000 loops, best of 3: 11.2 us per loop</p> <p>image_box2: memoryview: 100000 loops, best of 3: 18.1 us per loop</p> <p>These measurements are done from IPython using %timeit image_box1(im, pd, box_half_size)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload