大數我有我的Python 2.7的代碼下面的列表中理解它返回的行數(指數)和線一長串行:用於/中/如果列表綜合變得很慢火柴
results = [[lines.index(line), line] for line in lines
if search_item in line.lower()]
這是閃電般的速度,如果結果的數量低:
The search item is: [ 1330 ]
Before string pre-processing, the time is: 0.0000
The number of lines is: 1,028,952
After string pre-processing, the time is: 0.2500
The number of results is: 249
「字符串前處理」是什麼東西,我呼籲上述結果=操作。
下面是相同的操作,但將「1330」作爲搜索項目而不是「1330」。這其中產生了6,049場比賽,而不是249:
The search item is: [1330]
Before string pre-processing, the time is: 0.0000
The number of lines is: 1,028,952
After string pre-processing, the time is: 10.3180
The number of results is: 6,049
正如你可以看到,10秒與1/4秒......此外,「1330」和「1330」的搜索在2.4和3.2秒分別使用運行一個循環:
for lineNum, line in enumerate(lines):
if search_item in line.lower():
return lineNum, line
所以,列表理解給出的性能提高了10倍的249個結果的情況下,但3個+ X較慢6,049結果...
顯然,這個問題是不是在列表理解的if/in部分(這兩個搜索都掃描所有1M +行並接受或拒絕eac小時),但是在第二種情況下建立一個「長期」的結果列表。換句話說,瓶頸似乎在
results = [lines.index(line), line]
部分的理解。
我猜我很驚訝列表理解對於大型結果集來說太慢了(而6K實際上並不那麼大)。我錯過了什麼?有沒有一種我應該使用的方法會持續超越for循環?
您已經知道'enumerate()'。你爲什麼不在列表理解中使用它? –
不理解list.index()的開銷。另外,對於Python還是很新穎的,特別是列出理解。但是,現在我在我的應用程序中進行了超快速搜索。很興奮! – MichaelA