在 Python 中, read() 或 readlines() 更快吗? [英] In Python, is read() , or readlines() faster?
问题描述
我想在我的代码中读取一个大文件.read() 或 readline() 为此更快.循环如何:
I want to read a huge file in my code. Is read() or readline() faster for this. How about the loop:
for line in fileHandle
推荐答案
对于文本文件,只需使用 for
循环遍历它几乎总是可行的方法.别在意速度,它是最干净的.
For a text file just iterating over it with a for
loop is almost always the way to go. Never mind about speed, it is the cleanest.
在某些版本的 python readline()
确实只读取一行,而 for
循环读取大块并将它们分成几行,所以它可能更快.我认为更新版本的 Python 也对 readline()
使用缓冲,因此性能差异将是微不足道的(for
可能仍然在微观上更快,因为它避免了方法调用).然而,出于性能原因选择一种而不是另一种可能是过早的优化.
In some versions of python readline()
really does just read a single line while the for
loop reads large chunks and splits them up into lines so it may be faster. I think that more recent versions of Python use buffering also for readline()
so the performance difference will be minuscule (for
is probably still microscopically faster because it avoids a method call). However choosing one over the other for performance reasons is probably premature optimisation.
编辑添加:我刚刚查看了一些 Python 发行说明.Python 2.5 说:
Edit to add: I just checked back through some Python release notes. Python 2.5 said:
现在混合迭代是非法的文件中带有 for 行的文件和调用文件对象的read()/readline()/readlines() 方法.
It’s now illegal to mix iterating over a file with for line in file and calling the file object’s read()/readline()/readlines() methods.
Python 2.6 引入了 TextIOBase,它同时支持迭代和 readline()
.
Python 2.6 introduced TextIOBase which supports both iterating and readline()
simultaneously.
Python 2.7 固定交错 read()
和 readline()
.
Python 2.7 fixed interleaving read()
and readline()
.
这篇关于在 Python 中, read() 或 readlines() 更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!