[Numpy-discussion] Speed performance on array constant set

Sasha ndarray at mac.com
Thu Jan 19 19:53:02 CST 2006


On 1/19/06, Mark Heslep <mark at mitre.org> wrote:
> A >10x hit.   Am I using the preferred / optimal NumPy method here?  I
> scanned the earlier Scalar  posts but thought that was boolean  type
> only issue.

Try a.fill(7).

On my system:

> python -m timeit  -s "import numpy as na; a = na.zeros((1000,1000))" "a[:,:] = 7"
100 loops, best of 3: 17.1 msec per loop

> python -m timeit  -s "import numpy as na; a = na.zeros((1000,1000))" "a.fill(7)"
100 loops, best of 3: 6.28 msec per loop

You can also use empty instead of zeros, but that wont affect the
timings that you presented.

-- sasha




More information about the Numpy-discussion mailing list