On Thu, Feb 20, 2020 at 2:11 PM Chris Angelico <rosuav@gmail.com> wrote:
On Fri, Feb 21, 2020 at 8:52 AM Stephan Hoyer <shoyer@gmail.com> wrote:
> On Thu, Feb 20, 2020 at 12:41 PM Steve Jorgensen <stevej@stevej.name> wrote:
>> Christopher Barker wrote:
>> ...
>> > > Perhaps the OP wanted the internal array size initialized, but not used.
>> > Currently the internal array will automatically be reallocated to grow as
>> > needed. Which could be a performance hit if you know it’s going to grow
>> > large.
>> > But frankly, it would be a rare case where this would be noticeable.
>> > -CHB
>> Maybe uncommon, but I don't know about rare. Let's say you want to perform list-wise computations, making new lists with results of operations on existing lists (similar to numpy, but maybe trying to do something numpy is unsuitable for)? You would want to pre-allocate the new array to the size of the operand arrays.
> 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.