[Numpy-discussion] Faster
Robert Kern
robert.kern@gmail....
Sat May 3 19:20:34 CDT 2008
On Sat, May 3, 2008 at 7:05 PM, Christopher Barker
<Chris.Barker@noaa.gov> wrote:
> 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.
After fixing the last statement to "out[i:,i:] = ...", yes, much
faster. About a factor of 5 with N=500.
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the Numpy-discussion
mailing list