<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 7 August 2016 at 15:08, Michael Selik <span dir="ltr"><<a href="mailto:michael.selik@gmail.com" target="_blank">michael.selik@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><br><br><div class="gmail_quote"><span class=""><div dir="ltr">On Sat, Aug 6, 2016 at 8:45 PM INADA Naoki <<a href="mailto:songofacandy@gmail.com" target="_blank">songofacandy@gmail.com</a>> wrote:</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
1. bytes(bytearray[:n])<br>
2. bytes(memoryview(bytearray)[:<wbr>n])<br>
<br>
(1) is simplest, but it produces temporary bytearray having n bytes.<br></blockquote><div><br></div></span><div>Does that actually make the difference between unacceptably inefficient performance and acceptably efficient for an application you're working on?</div><span class=""><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
While (2) is more efficient than (1), it uses still temporary memoryview<br>
object, and it looks bit tricky.<br></blockquote><div><span style="line-height:1.5"><br></span></div></span><div><span style="line-height:1.5">Using the memoryview is nicely explicit whereas ``bytes.frombuffer`` could be creating a temporary bytearray as part of its construction.</span></div></div></div></blockquote><div><br></div>It could, but it wouldn't (since that would be pointlessly inefficient).<br><br></div><div class="gmail_quote">The main question to be answered here would be whether adding a dedicated spelling for "<span class="im">bytes(memoryview(bytearray)[:<wbr>n])" actually smooths out the learning curve for memoryview in general, where folks would learn:<br><br></span></div><div class="gmail_quote"><span class="im">1. "bytes(mybytearray[:n])" copies the data twice for no good reason<br></span></div><div class="gmail_quote"><span class="im">2. "bytes.frombuffer(mybytearray, n)" avoids the double copy<br></span>3<span class="im">. "bytes(memoryview(mybytearray)[:n])" generalises to arbitrary slices<br><br></span></div><div class="gmail_quote"><span class="im">With memoryview being a builtin, I'm not sure that argument can be made successfully - the transformation in going from step 1 direct to step 3 is just "wrap the original object with memoryview before slicing to avoid the double copy", and that's no more complicated than using a different constructor method.<br></span></div><br></div><div class="gmail_extra">Cheers,<br></div><div class="gmail_extra">Nick.<br></div><div class="gmail_extra"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Nick Coghlan | <a href="mailto:ncoghlan@gmail.com" target="_blank">ncoghlan@gmail.com</a> | Brisbane, Australia</div>
</div></div>