众所周知,在 Haskell 中不使用 [Char]
读取大量数据。使用 ByteString 来完成这项工作。
对此的通常解释是 Char
很大并且列表增加了它们的开销。
但是,这似乎不会导致输出出现任何问题。
例如以下程序:
main = interact $ const $ unwords $ map show $ replicate 500000 38000000
在我的计算机上运行只需 131 毫秒,而以下一个:
import Data.List
sum' :: [Int] -> Int
sum' = foldl' (+) 0
main = interact $ show . sum' . map read . words
如果将第一个程序的输出作为输入,则需要 3.38 秒!
使用String
的输入和输出性能之间存在如此差异的原因是什么?
最佳答案
我认为这个问题不一定与 I/O 有关。相反,它表明 Int
的 Read
实例效率相当低。
首先,考虑以下仅处理惰性列表的程序。在我的机器上需要 4.1 秒(使用 -O2
编译):
main = print $ sum' $ map read $ words
$ unwords $ map show $ replicate 500000 38000000
用 length
替换 read
函数可将时间降至 0.48 秒:
main = print $ sum' $ map length $ words
$ unwords $ map show $ replicate 500000 38000000
此外,将read
函数替换为手写版本会导致时间为0.52秒:
main = print $ sum' $ map myread $ words
$ unwords $ map show $ replicate 500000 38000000
myread :: String -> Int
myread = loop 0
where
loop n [] = n
loop n (d:ds) = let d' = fromEnum d - fromEnum '0' :: Int
n' = 10 * n + d'
in loop n' ds
我猜测为什么 read
效率如此低下,因为它的实现使用了 Text.ParserCombinators.ReadP
模块,这可能不是简单情况下最快的选择读取单个整数。
关于string - 为什么 Haskell 中基于 [Char] 的输入比基于 [Char] 的输出慢得多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7510078/