何时使用 StringIO,而不是连接字符串列表?
使用 StringIO 作为字符串缓冲区比使用列表作为缓冲区慢。
什么时候使用StringIO?
from io import StringIO
def meth1(string):
a = []
for i in range(100):
a.append(string)
return ''.join(a)
def meth2(string):
a = StringIO()
for i in range(100):
a.write(string)
return a.getvalue()
if __name__ == '__main__':
from timeit import Timer
string = "This is test string"
print(Timer("meth1(string)", "from __main__ import meth1, string").timeit())
print(Timer("meth2(string)", "from __main__ import meth2, string").timeit())
结果:
16.7872819901
18.7160351276
Using StringIO as string buffer is slower than using list as buffer.
When is StringIO used?
from io import StringIO
def meth1(string):
a = []
for i in range(100):
a.append(string)
return ''.join(a)
def meth2(string):
a = StringIO()
for i in range(100):
a.write(string)
return a.getvalue()
if __name__ == '__main__':
from timeit import Timer
string = "This is test string"
print(Timer("meth1(string)", "from __main__ import meth1, string").timeit())
print(Timer("meth2(string)", "from __main__ import meth2, string").timeit())
Results:
16.7872819901
18.7160351276
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
StringIO 的主要优点是它可以在需要文件的地方使用。例如,你可以这样做(对于 Python 2):
The main advantage of StringIO is that it can be used where a file was expected. So you can do for example (for Python 2):
如果您测量速度,您应该使用
cStringIO
。来自文档:
但 StringIO 的要点是成为一个类似文件的对象,因为当某些东西需要这样并且您不想使用实际文件时。
编辑:我注意到你使用了
from io import StringIO
,所以你可能使用的是Python >= 3或至少2.6。单独的 StringIO 和 cStringIO 在 Py3 中消失了。不确定他们使用什么实现来提供 io.StringIO。还有 io.BytesIO 。If you measure for speed, you should use
cStringIO
.From the docs:
But the point of StringIO is to be a file-like object, for when something expects such and you don't want to use actual files.
Edit: I noticed you use
from io import StringIO
, so you are probably on Python >= 3 or at least 2.6. The separate StringIO and cStringIO are gone in Py3. Not sure what implementation they used to provide the io.StringIO. There isio.BytesIO
too.好吧,我不知道我是否愿意将其用作“缓冲区”,您只是以两种复杂的方式将字符串 a 乘以 100 次。这是一种简单的方法:
如果我们将其添加到您的测试中:
事实证明,作为奖励,它会更快:
如果您想创建一堆字符串,然后连接它们,meth1() 是正确的方法。将其写入 StringIO 是没有意义的,这是完全不同的东西,即具有类似文件流接口的字符串。
Well, I don't know if I would like to call that using it as a "buffer", you are just multiplying a string a 100 times, in two complicated ways. Here is an uncomplicated way:
If we add that to your test:
It turns out to be way faster as a bonus:
If you want to create a bunch of strings, and then join them, meth1() is the correct way. There is no point in writing it to StringIO, which is something completely different, namely a string with a file-like stream interface.
另一种方法基于 Lennart Regebro 方法。
这比列表方法 (meth1) 更快
结果(秒):
Another approach based on Lennart Regebro approach.
This is faster than list method (meth1)
Results (sec.):