In Python, is read() , or readlines() faster?

后端 未结 8 1660
醉酒成梦
醉酒成梦 2020-11-30 05:50

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
8条回答
  •  鱼传尺愫
    2020-11-30 06:33

    If you have enough memory use readline if performance is a concern. I have seen that while using a gzip file doing: read().split('\n') took 5 seconds to loop through, whereas using the iterator took 38 seconds. The size of GZ file was around 45 MB.

提交回复
热议问题