On May 8, 2009, at 10:27 , Dag Sverre Seljebotn wrote:

Brian Blais wrote:
Hello,

In my question for optimization of my routines, I am trying to pre- fill
a static c-array from a list of numpy ndarrays.  Previously, I did
something like:

cdef double *w[30]
cdef int r[30],c[30]

and then I'd run through my list, and get the shape info and the data
pointer, and fill in these arrays.  This works, but then I have to do
all my 2D -> 1D indexing, like:

w[k][i+c[k]*j]

which is ugly, and prone to typos.  I'd like to do:

So the underlying problem here is that each array has a (widely
different) size?

the underlying problem is that the difference between the following two pieces is about a factor of 20:

# using a list of objects, getting an array (called weights) from each one
# and doing some calculations
        cdef np.ndarray[DTYPE_t,ndim=2] weights

        for c in self.connections_to:
            num_incell=c.incell.N
            num_outcell=c.outcell.N
            weights=c.weights

            for __i in range(num_outcell):
                for __j in range(num_incell):
                    a+=weights[__i,__j]


and

# using an array of structures, prefilled from a list of objects,
# getting an pointer to the data (called weights) from each one
# and doing some calculations
    cdef double *weights

    for cg from 0<=cg<number_of_connections_to:
        num_incell=s.c_num_incell[cg]
        num_outcell=s.c_num_outcell[cg]
        weights=s.c_weights[cg]

        for ni from 0<=ni<num_incell:
            for no from 0<=no<num_outcell:
               a+=weights[no+ni*num_outcell]


#===========


I think this is because of the python calls in the former, and the number of times I call this little snippet. I am going to try to see if it is the running through the list, or the obtaining the attributes, which is the problem.


                        bb



You can always wrap the solution above into e.g. a cdef class, so that
you can write

yourspecialarrayobject.get(k, i, j)

that's a nice idea...  thanks.



                        bb

--
Brian Blais
[email protected]
http://web.bryant.edu/~bblais

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to