Efficient math ops on small arrays in python with cython

后端 未结 1 1685
执念已碎
执念已碎 2021-02-03 12:27

I use numpexpr for fast math on large arrays but if the size of the array is less than the CPU cache, writing my code in Cython using simple array math is way faster, especially

相关标签:
1条回答
  • 2021-02-03 12:49

    You can roll your simple own with basic functions and checks here is a mockup to start:

    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
    

    can be used as

    >>> import test
    >>> simple = test.SimpleArray(100)
    >>> del simple
    >>> test.running_sum(test.SimpleArray(100))
    <test.SimpleArray object at 0x1002a90b0>
    
    0 讨论(0)
提交回复
热议问题