Python binary related operations

10<<10 # 15.2 ns 仅整数
b'\x5f'.hex() # 111 ns
'香香香'.encode() # 139 ns 仅字符串
binascii.b2a_hex(b'\xb9\x01\xef') # 172 ns 仅字符串
struct.pack('>i',65535) # 211 ns 仅符合C语言标准结构
int('101',8) # 224 ns
bytes.fromhex('ff ff ff') # 231 ns 仅hex字符
int.to_bytes(1111,length=8,byteorder='little') # 260 ns 仅整数
bytearray.fromhex('ff ff ff') # 282 ns 仅hex字符
ctypes.string_at(i,g) # 1.13 µs 可以对内存中任何位置、任何长度进行操作
bin(a) #  3.03 µs 仅整数
bitstring.Bits(b'\x5f') # 10.1 µs
bitstring.Bits(b'\x5f').bin # 16.5 µs
bitstring.Bits(b'\x5f').hex # 29.3 µs

The bin that was contacted early, although it is a realistic bit situation, has the lowest efficiency. But the efficiency of int is very high.

Some bit-level operations, such as storage of position information, color information, etc. Usually byte-level operations are used a lot, if bit-level is used, it seems that there is no efficient method.

Byte is the smallest operation unit of the computer, and bit is the smallest storage unit of the computer

I realized that in this era when there is enough memory, it is cool to install X in bytes. If X is installed in bits, the memory usage may increase, but there is no good way and the conversion rate is low, so use bytes. enough!

Guess you like

Origin blog.csdn.net/jhsxy2005/article/details/114123956