Mar*_*ong 3 python performance file list
我有一个表示代码字节的整数列表.如何快速,高效地将它们写入二进制文件.
我试过了:
with open (output1, "wb") as compdata:
for row in range(height):
for data in cobs(delta_rows[row].getByte_List()):
output_stream.append(Bits(uint=data, length=8))
compdata.write(output_stream.tobytes())
Run Code Online (Sandbox Code Playgroud)
和
with open (output1, "wb") as compdata:
for row in range(height):
bytelist = cobs(delta_rows[row].getByte_List())
for byte in bytelist:
compdata.write(chr(byte))
Run Code Online (Sandbox Code Playgroud)
两个都给我一个我认为是正确的结果(我还没有扭转过程),但都需要很长时间(6分钟和4分钟).
使用bytearray()对象,直接写入输出文件:
with open (output1, "wb") as compdata:
for row in range(height):
bytes = bytearray(cobs(delta_rows[row].getByte_List()))
compdata.write(bytes)
Run Code Online (Sandbox Code Playgroud)
整数序列由a解释bytearray()为字节值序列.
在Python 3中,可以使用一个bytes()类型为好,用相同的输入; 毕竟,你不是在创造之后改变价值观.