我正在尝试同时使用 Python3 解析两个大文件。如此处所示:
dict = {}
row = {}
with open(file1, "r") as f1, open(file2, "r") as f2:
zipped = zip(f1, f2)
for line_f1, line_f2 in zipped:
# parse the lines and save the line information in a dictionary
row = {"ID_1":line_f1[0], "ID_2":line_f2[0], ...}
# This process takes roughly 0.0005s each time
# it parses each pair of lines at once and returns an output
# it doesn't depend on previous lines or lines after
output = process(row)
# output is a string, add it to dict
if output in dict:
dict[output] += 1
else:
dict[output] = 1
return dict
当我用两个较小的文本文件(每个 30,000 行,文件大小 = 13M)测试上述代码时,完成循环大约需要 150 秒。
当我使用两个大文本文件(每个文件 9,000,000 行,文件大小 = 3.8G)进行测试时,没有循环中的处理步骤,大约需要 670 秒。
当我在流程步骤中使用相同的两个大文本文件进行测试时。我计算出每 10,000 件物品大约需要 60 秒。当迭代次数变大时,时间并没有增长。
但是,当我将此作业提交到共享集群时,一对大文件需要超过 36 个小时才能完成处理。我试图弄清楚是否有任何其他方式来处理文件,以便它可以更快。任何建议,将不胜感激。
提前致谢!