1
只是試圖重寫瑣碎的文件分析器從pythonto haskell,但發現它痛苦緩慢(約15倍在同一臺機器上)。用ghc -O2編譯的代碼。緩慢的文件解析與正則表達式
目標是計算正則表達式匹配的行數。 示例文本文件很大(幾個GiB)。
下面是代碼:
import Text.Regex.Posix
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BC
filename = "sample.dat"
mcount' :: String -> [BS.ByteString] -> Int
mcount' sample file = foldr (\e acc -> if e =~ sample then acc+1 else acc) 0 file
main = do
fcnt <- fmap BC.lines $ BS.readFile filename
print $ mcount' "myregex" fcnt
我怎麼能(顯著)提高性能?
從'foldr'切換到'foldl''。可能編譯正則表達式會有所幫助。我並沒有把這個作爲答案,因爲我沒有真正分析過它。 –
@DanielWagner你不需要描述那個。這個問題只有很大的收穫。 –
哦,對於一個巨大的文本文件,你絕對**應該使用**'Data.ByteString.Lazy [.Char8]'**。如果不需要,您不需要將整個多GB文件同時存儲在內存中。 –