Arthur Darcet added the comment:

BytesIO is heavily optimised to avoid copying bytes when it can.
For case (1), if you want to modify the data, then there is no need to actually 
copy it before overwriting it, because no-one else is using it

For case (2), if you want to change something, then you need to copy it first, 
otherwise the original bytes object would get modified


Case (1):
% python -m timeit -s "import io; b = io.BytesIO(b'0' * 2 ** 30)" 
"b.getbuffer()"
1000000 loops, best of 3: 0.201 usec per loop


Case (2):
python -m timeit -s "import io; a = b'0' * 2 ** 30; b = io.BytesIO(a)" 
"b.getbuffer()"
10 loops, best of 3: 54.5 msec per loop

----------
versions:  -Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue31025>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to