什么是“更好”,多次调用使用小字符串写入文件或构建字符串以在一端调用一个文件?
# multiple small strings
with open('example1.txt', 'w') as f:
for i in some_data:
s = some_function(i)
f.write(s)
if some_condition(i):
f.write(str(i))
f.write('\n')
# one long string
res = ''
for i in some_data:
s = some_function(i)
res += s
if some_condition(i):
res += str(i)
res += '\n'
with open('example2.txt', 'w') as f:
f.write(res)
假设数据块i
为float
。
答案 0 :(得分:1)
如果你问哪种方法“更好”,我会回答这些问题:
非常非常非常罕见,您会遇到需要回答另一个问题的问题:
这不到1%的时间。不要以为这是你的情况,除非你非常非常有理由相信它。在这种情况下,仍然可能不是你的情况。关于你的情况,你将需要进行一些测试。有很多事情会影响你的答案,包括你拥有的内存量,内存速度,CPU速度,CPU缓存大小,L2缓存大小,性能你的硬盘。对于这两种不同的方法,两种不同的机器很可能表现不同。
您需要通过实现两个版本并运行数百,数千,数百万次迭代(根据您的具体情况)来运行测试
您还可能遇到写一个非常大的文件的情况。如果你的文本文件是15GB并且你的设备上只有4GB的RAM,那么首先在内存中构建字符串的速度要快多少并不重要,你需要逐行(或大块)流出它通过块)到磁盘。
一般情况下,尽可能选择简单的解决方案。不要浪费时间问自己,与另一种方式相比,它是否会更有效率。
当然,如果有疑问,您可以随时运行import this
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!