[Numpy-discussion] fast duplicate of array
Charles R Harris
charlesr.harris@gmail....
Sat Jan 23 18:08:37 CST 2010
On Sat, Jan 23, 2010 at 4:00 PM, Keith Goodman <kwgoodman@gmail.com> wrote:
> On Sat, Jan 23, 2010 at 2:31 PM, Alan G Isaac <aisaac@american.edu> wrote:
> > On 1/23/2010 5:01 PM, Anne Archibald wrote:
> >> If both arrays are "C contiguous", or more generally contiguous blocks
> >> of memory with the same strided structure, you might get faster
> >> copying by flattening them first, so that it can go in a single
> >> memcpy().
> >
> > I may misuderstand this. Did you just mean
> > x.flat = y.flat
> > ?
> >
> > If so, I find that to be *much* slower.
> >
> > Thanks,
> > Alan
> >
> >
> > x = np.random.random((1000,1000))
> > y = x.copy()
> > t0 = time.clock()
> > for t in range(1000): x = y.copy()
> > print(time.clock() - t0)
> > t0 = time.clock()
> > for t in range(1000): x[:,:] = y
> > print(time.clock() - t0)
> > t0 = time.clock()
> > for t in range(1000): x.flat = y.flat
> > print(time.clock() - t0)
>
> I don't know what a view is, but it is fast:
>
> x = y.view()
>
>
In this case x isn't a copy of y, it is a reference to the same data in
memory. It is fast because no copying is done.
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100123/a9ab2d5b/attachment-0001.html
More information about the NumPy-Discussion
mailing list