I just recently used memoryview to answer this question.
Bytearray is a mutable byte sequence, relative to str in Python2, but str is immutable. In Python3, since str is unicode encoding by default, it can only be accessed by bytes through bytearray.
Memoryview provides a byte-by-byte memory access interface for objects that support buffer protocol[1,2]. The advantage is that there is no memory copy. Str and bytearray support buffer procotol by default. Comparison of the following two behaviors: To put it simply, the slicing operation of str and bytearray will generate new slices str and bytearray and copy the data, but it will not happen after using memoryview.
Do not use memoryview
>> a = 'aaaaaa'
>> b = a[:2] # 会产生新的字符串
>> a = bytearray('aaaaaa')
>> b = a[:2] # 会产生新的bytearray
>> b[:2] = 'bb' # 对b的改动不影响a
>> a
bytearray(b'aaaaaa')
>> b
bytearray(b'bb')
My usage scenario is socket reception and analysis of received data in network programs:
The sock receiving code before using memoryview is simplified as follows
def read(size):
ret = ''
remain = size
while True:
data = sock.recv(remain)
ret += data # 这里不断会有新的str对象产生
if len(data) == remain:
break
remain -= len(data)
return ret
After using meoryview, we avoid constant string splicing and the generation of new objects
def read(size):
ret = memoryview(bytearray(size))
remain = size
while True:
data = sock.recv(remain)
length = len(data)
ret[size - remain: size - remain + length] = data
if len(data) == remain:
break
remain -= len(data)
return ret
There is another advantage of returning memoryview. When using struct for unpack parsing, you can directly receive the memoryview object, which is very efficient (avoiding a large number of slicing operations when parsing large str in segments).
For example:
mv = memoryview('\x00\x01\x02\x00\x00\xff...')
type, len = struct.unpack('!BI', mv[:5])
...
I just recently used memoryview to answer this question.
Bytearray is a mutable byte sequence, relative to str in Python2, but str is immutable.
In Python3, since str is unicode encoding by default, it can only be accessed by bytes through bytearray.
Memoryview provides a byte-by-byte memory access interface for objects that support buffer protocol[1,2]. The advantage is that there is no memory copy.
Str and bytearray support buffer procotol by default.
Comparison of the following two behaviors:
To put it simply, the slicing operation of str and bytearray will generate new slices str and bytearray and copy the data, but it will not happen after using memoryview.
Do not use memoryview
Use memoryview
My usage scenario is socket reception and analysis of received data in network programs:
The sock receiving code before using memoryview is simplified as follows
def read(size):
After using meoryview, we avoid constant string splicing and the generation of new objects
There is another advantage of returning memoryview. When using struct for unpack parsing, you can directly receive the memoryview object, which is very efficient (avoiding a large number of slicing operations when parsing large str in segments).
For example:
[1] https://jakevdp.github.io/blo...
[2] http://legacy.python.org/dev/...