在我目前的工作中,总是需要把生成的list写入文件或者从文件读出来,文件小的时候还没什么的,但是文件可大的时候简直慢的一逼。所以必须优化一下。以下是我找的一些读写文件的方法,希望通过这个可以清楚的描述出哪种方式更好.
#!/usr/bin/env python
# encoding: utf-8
'''
@author: shd
@time: 2019/10/19 13:43
@desc:
'''
import time
def calctime(func):
def wrapper(*args,**kwargs):
start_time = time.time()
result = func(*args,**kwargs)
end_time = time.time()
print("{}一共耗时{}".format(func.__name__,end_time-start_time))
return result
return wrapper
tmp = set(map(lambda x:str(x),range(100000)))
@calctime
def func_one():
with open('one.txt','w') as pf:
pf.write('\n'.join(tmp))
@calctime
def func_two():
for item in tmp:
with open('two.txt','a') as pf:
pf.write(item+'\n')
@calctime
def func_three():
with open('three.txt','w') as pf:
pf.writelines("%s\n" % t for t in tmp)
@calctime
def func_four():
with open('one.txt','r') as pf:
tmp = pf.read().strip().split('\n')
# print(tmp)
# print(len(tmp))
@calctime
def func_five():
with open('one.txt', 'r') as pf:
# for line in pf:
# print(line)
print(len([line for line in pf]))
if __name__ == '__main__':
func_one()
func_two()
func_three()
func_four()
func_five()
#output
func_one一共耗时0.03182792663574219
func_two一共耗时14.9568350315094
func_three一共耗时0.0598902702331543
func_four一共耗时0.010167121887207031
func_five一共耗时0.014065265655517578
网友评论