[Numpy-tickets] [NumPy] #411: indices function too slow

NumPy numpy-tickets@scipy....
Sat Mar 31 18:46:08 CDT 2007


#411: indices function too slow
-------------------------+--------------------------------------------------
 Reporter:  auger        |        Owner:  somebody   
     Type:  enhancement  |       Status:  closed     
 Priority:  normal       |    Milestone:  1.0 Release
Component:  numpy.core   |      Version:             
 Severity:  normal       |   Resolution:  fixed      
 Keywords:  indices      |  
-------------------------+--------------------------------------------------
Changes (by oliphant):

  * status:  new => closed
  * resolution:  => fixed

Old description:

> The indices function is quite slow (~12s to create indices for a
> 4000x4000 array on my machine). Here is a version that is between 4 and 8
> times faster (depending on the array sizes).
>

> {{{
> def indices(shape,dtype=int):
>         lshape = []
>         oshape = [len(shape)]
>         for i in shape:
>                 lshape.append(i)
>                 oshape.append(i)
>         out = empty(tuple(oshape))
>         dims = len(lshape)
>         for i in range(dims):
>                 v = lshape.pop(i)
>                 lshape.append(1)
>                 subshape = tuple(lshape)
>                 coords = arange(shape[i])
>                 tiled = tile(coords,subshape)
>                 ndims = tiled.ndim
>                 tindx = arange(ndims)
>                 tindx[tindx>i] -= 1
>                 tindx[i] = -1
>                 lshape.pop()
>                 lshape.insert(i,v)
>                 out[i] = tiled.transpose(tindx)
>         return out.astype(dtype)
> }}}

New description:

 The indices function is quite slow (~12s to create indices for a 4000x4000
 array on my machine). Here is a version that is between 4 and 8 times
 faster (depending on the array sizes).


 {{{
 def indices(shape,dtype=int):
         lshape = []
         oshape = [len(shape)]
         for i in shape:
                 lshape.append(i)
                 oshape.append(i)
         out = empty(tuple(oshape))
         dims = len(lshape)
         for i in range(dims):
                 v = lshape.pop(i)
                 lshape.append(1)
                 subshape = tuple(lshape)
                 coords = arange(shape[i])
                 tiled = tile(coords,subshape)
                 ndims = tiled.ndim
                 tindx = arange(ndims)
                 tindx[tindx>i] -= 1
                 tindx[i] = -1
                 lshape.pop()
                 lshape.insert(i,v)
                 out[i] = tiled.transpose(tindx)
         return out.astype(dtype)
 }}}

Comment:

 I've replaced the indices function with a version that is substantially
 faster.

 In r3627

-- 
Ticket URL: <http://projects.scipy.org/scipy/numpy/ticket/411#comment:1>
NumPy <http://projects.scipy.org/scipy/numpy>
The fundamental package needed for scientific computing with Python.


More information about the Numpy-tickets mailing list