Python 如何从大文件快速创建数组?
我举了一个例子:Python 如何从大文件快速创建数组?,python,Python,我举了一个例子: for line in IN.readlines(): line = line.rstrip('\n') mas = line.split('\t') row = ( int(mas[0]), int(mas[1]), mas[2], mas[3], mas[4] ) self.inetnums.append(row) IN.close() 如果ffilesize==120mb,脚本时间=10秒。
for line in IN.readlines():
line = line.rstrip('\n')
mas = line.split('\t')
row = ( int(mas[0]), int(mas[1]), mas[2], mas[3], mas[4] )
self.inetnums.append(row)
IN.close()
如果ffilesize==120mb,脚本时间=10秒。我可以减少这一时间吗?删除readlines()
照办
for line in IN:
使用readlines
可以创建文件中所有行的列表,然后访问每一行,而无需执行此操作。没有它,for循环只使用生成器,它每次从文件返回一行。删除readlines()
照办
for line in IN:
使用
readlines
可以创建文件中所有行的列表,然后访问每一行,而无需执行此操作。如果没有它,for循环只使用生成器,它每次从文件返回一行。如果使用列表,您可能会获得一些速度
inetnums=[(int(x) for x in line.rstrip('\n').split('\t')) for line in fin]
以下是两个不同版本的配置文件信息
>>> def foo2():
fin.seek(0)
inetnums=[]
for line in fin:
line = line.rstrip('\n')
mas = line.split('\t')
row = ( int(mas[0]), int(mas[1]), mas[2], mas[3])
inetnums.append(row)
>>> def foo1():
fin.seek(0)
inetnums=[[int(x) for x in line.rstrip('\n').split('\t')] for line in fin]
>>> cProfile.run("foo1()")
444 function calls in 0.004 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.003 0.003 0.004 0.004 <pyshell#362>:1(foo1)
1 0.000 0.000 0.004 0.004 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
220 0.000 0.000 0.000 0.000 {method 'rstrip' of 'str' objects}
1 0.000 0.000 0.000 0.000 {method 'seek' of 'file' objects}
220 0.000 0.000 0.000 0.000 {method 'split' of 'str' objects}
>>> cProfile.run("foo2()")
664 function calls in 0.006 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.005 0.005 0.006 0.006 <pyshell#360>:1(foo2)
1 0.000 0.000 0.006 0.006 <string>:1(<module>)
220 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
220 0.001 0.000 0.001 0.000 {method 'rstrip' of 'str' objects}
1 0.000 0.000 0.000 0.000 {method 'seek' of 'file' objects}
220 0.001 0.000 0.001 0.000 {method 'split' of 'str' objects}
>>>
>>def foo2():
fin.seek(0)
inetnums=[]
对于fin中的行:
line=line.rstrip('\n')
mas=line.split('\t')
行=(int(mas[0]),int(mas[1]),mas[2],mas[3])
inetnums.append(行)
>>>def foo1():
fin.seek(0)
inetnums=[[int(x)代表第x行。rstrip('\n')。split('\t')]代表第fin行]
>>>cProfile.run(“foo1()”)
0.004 CPU秒内444次函数调用
订购人:标准名称
ncalls tottime percall cumtime percall文件名:lineno(函数)
1 0.003 0.003 0.004 0.004:1(foo1)
1 0.000 0.000 0.004 0.004 :1()
1 0.000 0.000 0.000 0.000{方法'disable'的''lsprof.Profiler'对象}
220 0.000 0.000 0.000 0.000{“str”对象的方法“rstrip”}
1 0.000 0.000 0.000 0.000{“文件”对象的“查找”方法}
220 0.000 0.000 0.000 0.000{“str”对象的“拆分”方法}
>>>cProfile.run(“foo2()”)
在0.006 CPU秒内调用664个函数
订购人:标准名称
ncalls tottime percall cumtime percall文件名:lineno(函数)
1 0.005 0.005 0.006 0.006:1(foo2)
1 0.000 0.000 0.006 0.006 :1()
220 0.000 0.000 0.000 0.000{“列表”对象的“附加”方法}
1 0.000 0.000 0.000 0.000{方法'disable'的''lsprof.Profiler'对象}
220 0.001 0.000 0.001 0.000{“str”对象的方法“rstrip”}
1 0.000 0.000 0.000 0.000{“文件”对象的“查找”方法}
220 0.001 0.000 0.001 0.000{“str”对象的“拆分”方法}
>>>
如果使用列表,您可能会获得一些速度
inetnums=[(int(x) for x in line.rstrip('\n').split('\t')) for line in fin]
以下是两个不同版本的配置文件信息
>>> def foo2():
fin.seek(0)
inetnums=[]
for line in fin:
line = line.rstrip('\n')
mas = line.split('\t')
row = ( int(mas[0]), int(mas[1]), mas[2], mas[3])
inetnums.append(row)
>>> def foo1():
fin.seek(0)
inetnums=[[int(x) for x in line.rstrip('\n').split('\t')] for line in fin]
>>> cProfile.run("foo1()")
444 function calls in 0.004 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.003 0.003 0.004 0.004 <pyshell#362>:1(foo1)
1 0.000 0.000 0.004 0.004 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
220 0.000 0.000 0.000 0.000 {method 'rstrip' of 'str' objects}
1 0.000 0.000 0.000 0.000 {method 'seek' of 'file' objects}
220 0.000 0.000 0.000 0.000 {method 'split' of 'str' objects}
>>> cProfile.run("foo2()")
664 function calls in 0.006 CPU seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.005 0.005 0.006 0.006 <pyshell#360>:1(foo2)
1 0.000 0.000 0.006 0.006 <string>:1(<module>)
220 0.000 0.000 0.000 0.000 {method 'append' of 'list' objects}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
220 0.001 0.000 0.001 0.000 {method 'rstrip' of 'str' objects}
1 0.000 0.000 0.000 0.000 {method 'seek' of 'file' objects}
220 0.001 0.000 0.001 0.000 {method 'split' of 'str' objects}
>>>
>>def foo2():
fin.seek(0)
inetnums=[]
对于fin中的行:
line=line.rstrip('\n')
mas=line.split('\t')
行=(int(mas[0]),int(mas[1]),mas[2],mas[3])
inetnums.append(行)
>>>def foo1():
fin.seek(0)
inetnums=[[int(x)代表第x行。rstrip('\n')。split('\t')]代表第fin行]
>>>cProfile.run(“foo1()”)
0.004 CPU秒内444次函数调用
订购人:标准名称
ncalls tottime percall cumtime percall文件名:lineno(函数)
1 0.003 0.003 0.004 0.004:1(foo1)
1 0.000 0.000 0.004 0.004 :1()
1 0.000 0.000 0.000 0.000{方法'disable'的''lsprof.Profiler'对象}
220 0.000 0.000 0.000 0.000{“str”对象的方法“rstrip”}
1 0.000 0.000 0.000 0.000{“文件”对象的“查找”方法}
220 0.000 0.000 0.000 0.000{“str”对象的“拆分”方法}
>>>cProfile.run(“foo2()”)
在0.006 CPU秒内调用664个函数
订购人:标准名称
ncalls tottime percall cumtime percall文件名:lineno(函数)
1 0.005 0.005 0.006 0.006:1(foo2)
1 0.000 0.000 0.006 0.006 :1()
220 0.000 0.000 0.000 0.000{“列表”对象的“附加”方法}
1 0.000 0.000 0.000 0.000{方法'disable'的''lsprof.Profiler'对象}
220 0.001 0.000 0.001 0.000{“str”对象的方法“rstrip”}
1 0.000 0.000 0.000 0.000{“文件”对象的“查找”方法}
220 0.001 0.000 0.001 0.000{“str”对象的“拆分”方法}
>>>
您正在将一个120GB的文件读入内存?您的机器有多少内存?什么硬盘驱动器有12GB/秒?您正在将120GB的文件读入内存?您的机器有多少内存?什么硬盘驱动器的速度是12GB/秒?除了删除读线所获得的速度外,您是否会通过使用列表补偿来实际获得一些速度?在我看来,它只是编写同一代码的另一种方式。jjaLak:考虑一个事实,即你不会在一个循环中多次调用附加。我已经用cProfile中的信息更新了我的答案。除了删除读线所获得的速度之外,您是否真的可以通过使用列表comps获得一些速度?在我看来,它只是编写同一代码的另一种方式。jjaLak:考虑一个事实,即你不会在一个循环中多次调用附加。我已经用cProfile中的信息更新了我的答案。