我一直试图在haskell中读取一个大文件。
我需要为大学项目使用自定义算法对其进行压缩。一切正常,直到我开始压缩大文件。
我从我的程序中提取出了问题所在,并以“Hello big file”的形式在这里公开它:
import System
import qualified Data.ByteString.Lazy as BL
import Data.Word
fold_tailrec :: (a -> b -> a) -> a -> [b] -> a
fold_tailrec _ acc [] =
acc
fold_tailrec foldFun acc (x : xs) =
fold_tailrec foldFun (foldFun acc x) xs
fold_tailrec' :: (a -> b -> a) -> a -> [b] -> a
fold_tailrec' _ acc [] =
acc
fold_tailrec' foldFun acc (x : xs) =
let forceEval = fold_tailrec' foldFun (foldFun acc x) xs in
seq forceEval forceEval
main :: IO ()
main =
do
args <- System.getArgs
let filename = head args
byteString <- BL.readFile filename
let wordsList = BL.unpack byteString
-- wordsList is supposed to be lazy (bufferized)
let bytesCount = fold_tailrec (\acc word -> acc + 1) 0 wordsList
print ("Total bytes in " ++ filename ++ ": "
++ (show bytesCount))
我将此文件命名为 Test.hs,然后执行以下操作:
$ ls -l toto
-rwxrwxrwx 1 root root 5455108 2011-03-23 19:08 toto
$ ghc --make -O Test.hs
[1 of 1] Compiling Main ( Test.hs, Test.o )
Linking Test ...
$ ./Test toto
Stack space overflow: current size 8388608 bytes.
Use `+RTS -Ksize -RTS' to increase it.
$ ./Test toto +RTS -K50M -RTS
Stack space overflow: current size 50000000 bytes.
Use `+RTS -Ksize -RTS' to increase it.
$ ./Test toto +RTS -K500M -RTS
"Total bytes in toto: 5455108"
$ time ./Test toto +RTS -K500M -RTS
"Total bytes in toto: 5455108"
real 0m33.453s
user 0m8.917s
sys 0m10.433s
谁能解释一下为什么我需要 500 兆字节的 RAM 和 30 秒的 CPU 才能浏览一个可怜的 5 兆字节文件?请问我在做什么错?为什么 [word8] 没有像 ByteString 文档所述那样缓冲。以及如何解决这个问题?
我试图定义自己的尾递归折叠而不是 foldl、foldr 或 foldl'。我也尝试使用 seq 解冻 thunk。到目前为止我没有得到任何结果。
感谢您提供任何帮助,因为我被卡住了。