On Feb 21, 2020, at 00:46, Steven D'Aprano <steve@pearwood.info> wrote:
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.
Without reading the code, I can guess. The first one does two 500K allocations and a 500K memcpy; the second only does one 500K allocation but does 150K separate 4-byte copies, and the added cost of that loop and of not moving as many bytes at a time as possible is higher than the savings of a 500K allocation.