
On Thu, Feb 20, 2020 at 02:19:13PM -0800, Stephan Hoyer wrote:
Strong +1 for an array.zeros() constructor, and/or a lower level array.empty() which doesn't pre-fill values.
So it'd be a shorthand for something like this?
array.array("i", bytes(64)) array('i', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
It'd be convenient to specify the size as a number of array elements rather than bytes. But I'm not a heavy user of array.array() so I won't say either way as to whether this is needed.
Yes, exactly.
The main problem with array.array("i", bytes(64)) is that memory gets allocated twice, first to create the bytes() object and then to make the array(). This makes it unsuitable for high performance applications.
Got some actual measurements to demonstrate that initialising the array is a bottleneck? Especially for something as small as 64, it seems unlikely. If it were 64MB, that might be another story. What's wrong with `array.array("i", [0])*64` or equivalent? On my machine, at least, constructing a bytes object first followed by an array is significantly faster than the alternative: [steve@ando cpython]$ ./python -m timeit -s "from array import array" "array('i', bytes(500000))" 100 loops, best of 5: 1.71 msec per loop [steve@ando cpython]$ ./python -m timeit -s "from array import array" "array('i', [0])*500000" 50 loops, best of 5: 7.48 msec per loop That surprises me and I cannot explain it. -- Steven