Python/Cython: Overhead with classes stored in numpy arrays

This is the place for queries that don't fit in any of the other categories.

Python/Cython: Overhead with classes stored in numpy arrays

Postby loveandkindness » Sat Jun 14, 2014 2:50 pm

This slow code can be improved by changing the structure, but this is difficult to work around sometimes. The cause, I think, comes from classes stored in an array. I've heard memory views are used to link python and c arrays, but I'm still pretty new to this (only some python knowledge).

Is there a way to do the following efficiently?

An example class:

Code: Select all
cdef class ClassWithAdditionFunction:
    cdef double value

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

    cpdef add_one(self):
        self.value += 1

A slow function:

Code: Select all
cdef unsigned long int i, ii
cdef unsigned long int loops = pow(10, 8)
cdef double value

addition_classes = np.array([None] * 10)

for i in range(len(addition_classes)):
    addition_classes[i] = ClassWithAdditionFunction(value=0)

for i in range(loops/10):
    for ii in range(10):
        addition_classes[ii].add_one()


Thank you very much for any suggestions!
Last edited by Yoriz on Sat Jun 14, 2014 3:02 pm, edited 1 time in total.
Reason: First post lock.
loveandkindness
 
Posts: 1
Joined: Sat Jun 14, 2014 2:47 pm

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests