[Numpy-discussion] fast duplicate of array

Anne Archibald peridot.faceted at gmail.com
Sat Jan 23 17:01:11 EST 2010

2010/1/23 Alan G Isaac <aisaac at american.edu>:
> Suppose x and y are conformable 2d arrays.
> I now want x to become a duplicate of y.
> I could create a new array:
> x = y.copy()
> or I could assign the values of y to x:
> x[:,:] = y
> As expected the latter is faster (no array creation).
> Are there better ways?

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(). For really large arrays that use complete pages, some
low-level hackery involving memmap() might be able to make a shared
copy-on-write copy at almost no cost until you start modifying one
array or the other. But both of these tricks are intended for the
regime where copying the data is the expensive part, not fabricating
the array object; for that, I'm not sure you can accelerate things


More information about the NumPy-Discussion mailing list