在用Cython蟒蛇的小数组有效的数学OPS [英] Efficient math ops on small arrays in python with cython

查看:120
本文介绍了在用Cython蟒蛇的小数组有效的数学OPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用numpexpr快速数学上的大型阵列,但是,如果数组的大小小于CPU缓存,采用简单的数组运算写我的code地用Cython是远远快,特别是,如果函数被调用多个倍。

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, if the function is called multiple times.

问题是,你如何使用数组在用Cython工作,或者更明确:有没有直接接口在用Cython Python的array.array类型?我希望做的是这样的(简单的例子)

The issue is, how do you work with arrays in Cython, or more explicitly: is there a direct interface to Python's array.array type in Cython? What I would like to do is something like this (simple example)

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)

我第一次用用Cython numpy的包装尝试,并与ndarrays工作,但似乎他们创造是对小维数组非常昂贵,与创建C数组使用malloc(但内存处理成为一个痛)进行比较。

I first tried using Cython numpy wrapper and worked with the ndarrays but it seems that creating them is very costly for small 1D arrays, compared with creating a C array with malloc (but memory handling becomes a pain).

谢谢!

推荐答案

您可以滚你自己的简单的在这里的基本功能,并检查是启动样机:

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

可以用作

>>> import test
>>> simple = test.SimpleArray(100)
>>> del simple
>>> test.running_sum(test.SimpleArray(100))
<test.SimpleArray object at 0x1002a90b0>

这篇关于在用Cython蟒蛇的小数组有效的数学OPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆