高效数学运算使用cython在python中的小数组

chr*_*nos 9 python arrays performance numpy cython

我使用numpexpr在大型数组上进行快速数学运算,但如果数组的大小小于CPU缓存,则使用简单数组数学在Cython中编写代码会更快,尤其是如果多次调用该函数.

问题是,你如何在Cython中使用数组,或者更明确地说:在Cython中是否有Python的array.array类型的直接接口?我想做的是这样的事(简单例子)

cpdef array[double] running_sum(array[double] arr):
    cdef int i 
    cdef int n = len(arr)
    cdef array[double] out = new_array_zeros(1.0, n)
    ... # some error checks
    out[0] = arr[0]
    for i in xrange(1,n-1):
        out[i] = out[i-1] + arr[i]

    return(out)
Run Code Online (Sandbox Code Playgroud)

我首先尝试使用Cython numpy包装并使用ndarrays,但是与使用malloc创建C数组相比,创建它们对于小型1D数组来说似乎非常昂贵(但内存处理变得很麻烦).

谢谢!

fab*_*ioM 5

您可以使用基本功能滚动自己的代码,并从此处检查是否可以启动一个模型:

from libc.stdlib cimport malloc,free

cpdef class SimpleArray:
    cdef double * handle
    cdef public int length
    def __init__(SimpleArray self, int n):
        self.handle = <double*>malloc(n * sizeof(double))
        self.length = n
    def __getitem__(self, int idx):
        if idx < self.length:
            return self.handle[idx]
        raise ValueError("Invalid Idx")
    def __dealloc__(SimpleArray self):
        free(self.handle) 

cpdef SimpleArray running_sum(SimpleArray arr):
    cdef int i 
    cdef SimpleArray out = SimpleArray(arr.length)

    out.handle[0] = arr.handle[0]
    for i from 1 < i < arr.length-1:
        out.handle[i] = out.handle[i-1] + arr.handle[i]
    return out
Run Code Online (Sandbox Code Playgroud)

可以用作

>>> import test
>>> simple = test.SimpleArray(100)
>>> del simple
>>> test.running_sum(test.SimpleArray(100))
<test.SimpleArray object at 0x1002a90b0>
Run Code Online (Sandbox Code Playgroud)