方法一:執行時間較短,但耗費記憶體 (readlines)
f = open(filename,'r')
for line in f.readlines():
  if condition:
    do something
f.close()
方法二:省記憶體,執行時間較久 - 適合開大檔
f = open(filename,'r')
for line in f:
  if condition:
    do something
f.close()

 

下面的圖轉自 - Python: speed vs. memory tradeoff reading files -  isilanes on February 15, 2008


fileread_memory.png  

Fig. 1: Memory vs file size for both methods of reading the file

fileread_time.png 

Fig. 2: Execution time vs file size for both methods of reading the file

創作者介紹
創作者 kendoit 的頭像
kendoit

kendoit

kendoit 發表在 痞客邦 留言(0) 人氣( 11 )