mardi 5 mai 2015

Learning to write to an array in Cython

The simplified code of what I am tyring to do is much slower when I write to the "a" array:

in the pyx file:

import cython
import numpy as np
cimport numpy as np

ctypedef np.float64_t DTYPE_t

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.nonecheck(False)
def writearray(np.ndarray[DTYPE_t, ndim=1] a):

   cdef int i,j,k,l
   cdef DTYPE_t sum=0.0

   for i in range(100):
    for j in range(100):
        for k in range(100):
            for l in range(1000):
                sum+=1.0
            a[0]+=sum #this is the trouble line that makes the code slow.

I thought I have "a[0]" and "sum" to be the same type, but do I? Before this function is called, the "a" array is declared as

a=np.zeros(5, dtype=np.float64)

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire