我们可以在cython中有效地将对象从数组中拉出来吗?

时间:2014-06-19 00:54:32

标签: python performance optimization cython

我用cProfile计算了以下示例代码,结果就是这些。

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    3.283    3.283    3.283    3.283 {comparison.addition_with_arrayview}
        1    0.000    0.000    0.000    0.000 {comparison.simple_addition}

在其他人的帮助下,我设法使用对象,结构和其他方法使C语言接近C速度,但我找不到处理对象数组的方法。

有没有办法进一步减少在cython中从一个数组中拉出一个对象时产生的python开销? (我已经使用了cdef,就像我知道的那样。)

示例代码:

import numpy as np
cimport cython

@cython.boundscheck(False)
@cython.wraparound(False)

cdef class AdditionClass:
    cdef long int value

    def __init__(self, value):
        self.value = value

    cpdef int add_one(self):
        self.value += 1
        return 0

    def get_value(self):
        return self.value

cdef:
    int loops = 10
    long int a_big_number = pow(10, 7)

def simple_addition():
    cdef long int value = 0

    for i in range(loops):
        for ii in range(a_big_number):
            for iii in range(10):
                value += 1
        print "Loop ", i, " out of ", loops, '. Value: ', value
    return None

def addition_with_arrayview():
    cdef AdditionClass addInstance
    cdef long int value = 0

    addition_classes = np.array([None] * 10)
    for i in range(len(addition_classes)):
        addition_classes[i] = AdditionClass(0)

    cdef AdditionClass[:] arrayview = addition_classes

    for i in range(loops):
        for ii in range(a_big_number):
            for iii in range(10):
                addInstance = arrayview[iii]
                addInstance.add_one()

        print "Loop ", i, " our of ", loops, '. Value: ', \
            arrayview[i].get_value(), " split between 10 arrays."

    return None

个人资料脚本:

import cProfile
import comparison

def calling_all():
    comparison.simple_addition()
    comparison.addition_with_arrayview()

if __name__ == "__main__":
    cProfile.run('calling_all()', sort='time')

非常感谢你的时间和任何建议!

0 个答案:

没有答案