【问题标题】:Poor(er) performance of Cython with NumPy array memoryview compared to C arrays与 C 数组相比,带有 NumPy 数组 memoryview 的 Cython 性能较差
【发布时间】:2014-05-14 17:47:52
【问题描述】:

我遇到了来自benchmark 的一个非常奇怪的结果

这些都是不同风格的冒泡排序实现,在 n=10^4 时最快的方法是在内部将 Python 列表转换为 C 数组。相反,黄线对应于我使用带有 memoryview 的 NumPy 数组的代码。我预计结果会反过来。我(和同事)重复了几次基准测试并且总是得到相同的结果。也许有人知道这里发生了什么......

图中的黑线将对应代码:

%%cython
cimport cython
from libc.stdlib cimport malloc, free

def cython_bubblesort_clist(a_list):
    """ 
    The Cython implementation of bubble sort with internal
    conversion between Python list objects and C arrays.

    """
    cdef int *c_list
    c_list = <int *>malloc(len(a_list)*cython.sizeof(int))
    cdef int count, i, j # static type declarations
    count = len(a_list)

    # convert Python list to C array
    for i in range(count):
        c_list[i] = a_list[i]

    for i in range(count):
        for j in range(1, count):
            if c_list[j] < c_list[j-1]:
                c_list[j-1], c_list[j] = c_list[j], c_list[j-1]

    # convert C array back to Python list
    for i in range(count):
        a_list[i] = c_list[i]

    free(c_list)
    return a_list

和这段代码的粉色线:

%%cython
import numpy as np
cimport numpy as np
cimport cython
def cython_bubblesort_numpy(long[:] np_ary):
    """ 
    The Cython implementation of bubble sort with NumPy memoryview.

    """
    cdef int count, i, j # static type declarations
    count = np_ary.shape[0]

    for i in range(count):
        for j in range(1, count):
            if np_ary[j] < np_ary[j-1]:
                np_ary[j-1], np_ary[j] = np_ary[j], np_ary[j-1]

    return np.asarray(np_ary)

【问题讨论】:

  • 在您的基准测试中,您将记录时间的深度复制部分。虽然每个测试都是一样的,为什么不把它移到计时调用之外呢?在这种情况下,为什么您希望 numpy 更快?它的优势不在于访问每个元素,而在于执行整个数组操作。
  • 你看过下面的指南了吗,here and onward?
  • 添加这些装饰器并获得相同的性能。 @cython.boundscheck(False) @cython.wraparound(False)
  • 谢谢,成功了:)

标签: python arrays numpy cython


【解决方案1】:

按照上面 cmets 的建议,我添加了装饰器

%%cython
import numpy as np
cimport numpy as np
cimport cython
@cython.boundscheck(False) 
@cython.wraparound(False)
cpdef cython_bubblesort_numpy(long[:] np_ary):
    """ 
    The Cython implementation of bubble sort with NumPy memoryview.

    """
    cdef int count, i, j # static type declarations
    count = np_ary.shape[0]

    for i in range(count):
        for j in range(1, count):
            if np_ary[j] < np_ary[j-1]:
                np_ary[j-1], np_ary[j] = np_ary[j], np_ary[j-1]

    return np.asarray(np_ary)

结果更符合我现在的预期:)

【讨论】:

    【解决方案2】:

    值得对你的代码做一个微不足道的改变,看看它是否能进一步改进:

    cpdef cython_bubblesort_numpy(long[::1] np_ary):
        # ...
    

    这告诉 cython np_ary 是一个 C 连续 数组,嵌套的 for 循环中生成的代码可以使用此信息进一步优化。

    此代码不接受非连续数组作为参数,但使用numpy.ascontiguousarray() 处理起来相当简单。

    【讨论】:

    • 谢谢!但是,它不会编译,它不喜欢if np_ary[j] &lt; np_ary[j-1]:,我错过了什么吗?
    • 编译错误信息是什么,您使用的是什么版本的 Cython?我可以在这里编译它。
    • 版本 0.20.1,我把复制的贴在这里:pastebin.com/BL1Vj9ic 但我觉得我在这里遗漏了一些东西......
    • 没关系,我不知道为什么,但我只是看到我的代码中有一个 long[::-1] 而不是 long[::1] ;)
    • 基准测试的结果表明它确实提高了速度,但在 10^6 的样本量下甚至达不到 1%
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 2018-11-17
    相关资源
    最近更新 更多