[Numpy-discussion] Faster

Christopher Barker Chris.Barker@noaa....
Sat May 3 19:05:51 CDT 2008


Robert Kern wrote:
> I can get a ~20% improvement with the following:

> In [9]: def mycut(x, i):
>    ...:     A = x[:i,:i]
>    ...:     B = x[:i,i+1:]
>    ...:     C = x[i+1:,:i]
>    ...:     D = x[i+1:,i+1:]
>    ...:     return hstack([vstack([A,C]),vstack([B,D])])

Might it be a touch faster to built the final array first, then fill it:

def mycut(x, i):
     r,c = x.shape
     out = np.empty((r-1, c-1), dtype=x.dtype)
     out[:i,:i] = x[:i,:i]
     out[:i,i:] = x[:i,i+1:]
     out[i:,:i] = x[i+1:,:i]
     out[i:,i+1:] = x[i+1:,i+1:]
     return out

totally untested.

That should save the creation of two temporaries.

-Chris


-- 
Christopher Barker, Ph.D.
Oceanographer

NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception


More information about the Numpy-discussion mailing list