问题
Does converting from the mutable bytearray
type to the non-mutable bytes
type incur a copy? Is there any cost associated with it, or does the interpreter just treat it as an immutable byte sequence, like casting a char*
to a const char* const
in C++?
ba = bytearray()
ba.extend("some big long string".encode('utf-8'))
# Is this conversion free or expensive?
write_bytes(bytes(ba))
Does this differ between Python 3 where bytes
is its own type and Python 2.7 where bytes
is just an alias for str
?
回答1:
A new copy is created, the buffer is not shared between the bytesarray
and the new bytes
object, in either Python 2 or 3.
You couldn't share it, as the bytesarray
object could still be referenced elsewhere and mutate the value.
For the details, see the bytesobject.c source code, where the buffer protocol is used to create a straight up copy of the data (via PyBuffer_ToContiguous()).
回答2:
Martjin is right. I just wanted to back that answer up with the cpython source.
Looking at the source for bytes here, first bytes_new
is called, which will call PyBytes_FromObject
, which will call _PyBytes_FromBuffer
, which creates a new bytes object and calls PyBuffer_ToContiguous
(defined here). This calls buffer_to_contiguous
, which is a memory copy function. The comment for the function reads:
Copy src to a contiguous representation. order is one of 'C', 'F' (Fortran) or 'A' (Any). Assumptions: src has PyBUF_FULL information, src->ndim >= 1, len(mem) == src->len.
Thus, a call to bytes with a bytearray argument will copy the data.
来源:https://stackoverflow.com/questions/35880065/does-converting-from-bytearray-to-bytes-incur-a-copy