为了简单起见,让我们考虑现在写而不是读。
所以当你使用
open()像说:
with open("test.dat", "wb") as f: f.write(b"Hello World") f.write(b"Hello World") f.write(b"Hello World")
执行后,
test.dat将创建一个名为3x的文件
Hello World。数据写入文件后将不会保留在内存中(除非有名称保留)。
现在,当您考虑
io.BytesIO()改为:
with io.BytesIO() as f: f.write(b"Hello World") f.write(b"Hello World") f.write(b"Hello World")
它不是将内容写入文件,而是写入内存缓冲区。换句话说,一块RAM。本质上,编写以下内容将是等效的:
buffer = b""buffer += b"Hello World"buffer += b"Hello World"buffer += b"Hello World"
对于带有with语句的示例,最后还有一个del buffer
。
这里的主要区别是优化和性能。
io.BytesIO能够进行一些优化,使其比简单地将所有
b"Hello World"一个接一个的连接更快。
为了证明这一点,这里有一个小基准:
- Concat:1.3529秒
字节IO:0.0090秒
import io
import timebegin = time.time()
buffer = b”“
for i in range(0, 50000):
buffer += b”Hello World”
end = time.time()
seconds = end - begin
print(“Concat:”, seconds)begin = time.time()
buffer = io.BytesIO()
for i in range(0, 50000):
buffer.write(b”Hello World”)
end = time.time()
seconds = end - begin
print(“BytesIO:”, seconds)
除了提高性能外,使用
BytesIO代替连接还具有
BytesIO可以代替文件对象使用的优点。假设您有一个函数期望文件对象写入。然后,您可以为它提供内存中的缓冲区,而不是文件。
区别在于,
open("myfile.jpg","rb")仅加载并返回
myfile.jpg;的内容;而
BytesIO同样,它只是一个包含一些数据的缓冲区。
因为
BytesIO这只是一个缓冲区-如果您想稍后将内容写入文件-您必须执行以下 *** 作:
buffer = io.BytesIO()# ...with open("test.dat", "wb") as f: f.write(buffer.getvalue())
另外,您没有提到版本; 我正在使用Python3。与示例相关:我在使用with语句而不是调用f.close()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)