python write, writelines性能分析

来源:互联网 发布:教学教育过程最优化 编辑:程序博客网 时间:2024/06/09 05:24
准备数据:1G文本数据(共:5193374行)
1.write()
with open() as wf:
  wf.write(line)
性能分析:写数据耗时:13.094s
写入速度:6610.373708059671(行/秒

2.writelines()
with open() as wf:
  wf.writelines([line_list])
性能分析:写数据耗时:8.226s
若对line_list进行列表解析操作,遍历1G列表耗时:0.4s     (5,193,374行)
写入速度:10522.27490072129(行/秒

3.fileObj = open()
fileObj.write()
性能分析:写数据耗时:12.812s

对比1、3可知,with操作在对每行文件写操作完成以后有额外的操作:__exit__()将wf资源释放
0 0