Thursday, 15 January 2015

python - Can we efficiently pull objects out of an array in cython? -



python - Can we efficiently pull objects out of an array in cython? -

i've timed next sample code cprofile, , these results.

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}

with help of others, i've managed cython near c speeds using objects, structs, , other methods, can't find way deal array of objects.

is there way farther cut down python overhead incurred when pulling object out of array in cython? (i've used cdef much know how.)

sample code:

import numpy 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 homecoming 0 def get_value(self): homecoming self.value cdef: int loops = 10 long int a_big_number = pow(10, 7) def simple_addition(): cdef long int value = 0 in range(loops): ii in range(a_big_number): iii in range(10): value += 1 print "loop ", i, " out of ", loops, '. value: ', value homecoming none def addition_with_arrayview(): cdef additionclass addinstance cdef long int value = 0 addition_classes = np.array([none] * 10) in range(len(addition_classes)): addition_classes[i] = additionclass(0) cdef additionclass[:] arrayview = addition_classes in range(loops): ii in range(a_big_number): 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." homecoming none

profile script:

import cprofile import comparing def calling_all(): comparison.simple_addition() comparison.addition_with_arrayview() if __name__ == "__main__": cprofile.run('calling_all()', sort='time')

thank much time , advise!

python performance optimization cython

No comments:

Post a Comment