<p dir="ltr">On Jun 8, 2016 8:13 AM, "Paul Sokolovsky" <<a href="mailto:pmiscml@gmail.com">pmiscml@gmail.com</a>> wrote:<br>
><br>
> Hello,<br>
><br>
> On Wed, 8 Jun 2016 14:45:22 +0300<br>
> Serhiy Storchaka <<a href="mailto:storchaka@gmail.com">storchaka@gmail.com</a>> wrote:<br>
><br>
> []<br>
><br>
> > > $ ./run-bench-tests bench/bytealloc*<br>
> > > bench/bytealloc:<br>
> > >      3.333s (+00.00%) bench/bytealloc-1-bytes_n.py<br>
> > >      11.244s (+237.35%) bench/bytealloc-2-repeat.py<br>
> ><br>
> > If the performance of creating an immutable array of n zero bytes is<br>
> > important in MicroPython, it is worth to optimize b"\0" * n.<br>
><br>
> No matter how you optimize calloc + something, it's always slower than<br>
> just calloc.</p>
<p dir="ltr">`bytes(n)` *is* calloc + something. It's a lookup of and call to a global function. (Unless MicroPython optimizes away lookups for builtins, in which case it can theoretically optimize b"\0".__mul__.)</p>
<p dir="ltr">On the other hand, b"\0" is a constant, and * is an operator lookup that succeeds on the first argument (meaning, perhaps, a successful branch prediction). As a constant, it is only created once, so there's no intermediate object created.</p>
<p dir="ltr">AFAICT, the first requires optimizing global function lookups + calls, and the second requires optimizing lookup and *successful* application of __mul__ (versus failure + fallback to some __rmul__), and repetitions of a particular `bytes` object (which can be interned and checked against). That means there is room for either to win, depending on the efforts of the implementers.</p>
<p dir="ltr">(However, `bytearray` has no syntax for literals (and therefore easy constants), and is a more valid and, AFAIK, more practical concern.)</p>