python - cython memoryview 比预期慢

标签 python numpy cython memoryview

我已经开始在 cython 中使用 memoryviews 来访问 numpy 数组。它们具有的各种优点之一是它们比旧的 numpy 缓冲区支持快得多: http://docs.cython.org/src/userguide/memoryviews.html#comparison-to-the-old-buffer-support

但是,我有一个示例,其中旧的 numpy 缓冲区支持比 memoryviews 更快! 怎么会这样?!我想知道我是否正确使用了内存 View ?

这是我的测试:

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 = <int>(pd[0] + 0.5)  
    cdef unsigned int p1 = <int>(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 = <int>(pd[0] + 0.5)  
    cdef unsigned int p1 = <int>(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 

计时结果为:

image_box1:输入 numpy: 100000 次循环,最好的 3 次:每次循环 11.2 us

image_box2:内存 View : 100000 次循环,最好的 3 次:每次循环 18.1 us

这些测量是使用 %timeit image_box1(im, pd, box_half_size) 从 IPython 完成的

最佳答案

好的!我发现了问题。 正如 seberg 指出的那样,内存 View 看起来更慢,因为测量包括从 numpy 数组到内存 View 的自动转换。

我使用以下函数从 cython 模块中测量时间:

def test(params):   
    import timeit
    im = params[0]
    pd = params[1]
    box_half_size = params[2]
    t1 = timeit.Timer(lambda: image_box1(im, pd, box_half_size))
    print 'image_box1: typed numpy:'
    print min(t1.repeat(3, 10))
    cdef np.uint8_t[:, ::1] im2 = im
    cdef np.float64_t[:] pd2 = pd
    t2 = timeit.Timer(lambda: image_box2(im2, pd2, box_half_size))
    print 'image_box2: memoryview:'
    print min(t2.repeat(3, 10)) 

结果:

image_box1:输入 numpy: 9.07607864065e-05

image_box2:内存 View : 5.81799904467e-05

所以内存 View 确实更快!

请注意,我在调用 image_box2 之前将 im 和 pd 转换为 memoryviews。如果我不做这一步,直接传 im 和 pd,那么 image_box2 比较慢:

image_box1:输入 numpy: 9.12262257771e-05

image_box2:内存 View : 0.000185245087778

关于python - cython memoryview 比预期慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12800121/

相关文章:

python - 从系列中删除非数值

python - Cython 内存 View : wrapping c function with array parameter to pass numpy array

python - 如何使用正确的大小写获取 python 脚本的路径?

python - OpenAI Gym 在控制台环境中无法正确渲染颜色

python - 在 Python 3 中加载 Python 2 .npy 文件时出错

c++ - Cython 调用 MKL 通过 vdMul 在元素乘法上崩溃

python - Cython 中的并行性不起作用

python - 无法从 CreateView 重定向

python - 遍历包含字典的两个列表,将匹配附加到第三个列表

python - scipy.io.wavfile 给出 "WavFileWarning: chunk not understood"错误