
On Sat, Aug 6, 2016 at 8:45 PM INADA Naoki <songofacandy@gmail.com> wrote:
1. bytes(bytearray[:n]) 2. bytes(memoryview(bytearray)[:n])
(1) is simplest, but it produces temporary bytearray having n bytes.
Does that actually make the difference between unacceptably inefficient performance and acceptably efficient for an application you're working on?
While (2) is more efficient than (1), it uses still temporary memoryview object, and it looks bit tricky.
Using the memoryview is nicely explicit whereas ``bytes.frombuffer`` could be creating a temporary bytearray as part of its construction. The API I propose looks like this:
bytes.frombuffer(byteslike, length=-1, offset=0)
RawIOBase.read and the other read methods described in the io module use the parameter "size" instead of "length". https://docs.python.org/3/library/io.html#io.RawIOBase