为什么Haskell函数执行时间测量与ghc时序不同?

时间:2013-04-17 10:17:23

标签: performance haskell time

我想测量Haskell执行某些功能所花费的时间并使用TimeIt包(我也试过these推荐)。但是显示时间与实际应用时间不同(我使用+ RTS -sstderr选项运行应用程序):

CPU time:   4.85s
...  
  INIT    time    0.00s  (  0.00s elapsed)
  MUT     time    0.98s  ( 61.69s elapsed)
  GC      time    0.22s  (  0.19s elapsed)
  EXIT    time    0.00s  (  0.00s elapsed)
  Total   time    1.20s  ( 61.89s elapsed)

申请来源:

import qualified Data.ByteString.Lazy.Char8 as LBS
import System.Environment
import Data.Char
import Data.Int
import System.TimeIt

readChunks :: Int64 -> LBS.ByteString -> Int64
readChunks size str 
                            | LBS.null str = 0
                            | otherwise = let (chunk, rest) = LBS.splitAt size str
                                                 in do
                                                        let len = LBS.length chunk
                                                        len `seq` len + readChunks size rest



processFile :: String -> IO()
processFile name = do
    putStrLn name
    content <- LBS.readFile name
    let 
        (recNumStr, rest) = LBS.span (not.isControl) content
        recNum = LBS.readInt recNumStr
    case recNum of
        Nothing -> putStrLn "can't parse"
        Just (value, rest) -> print (value)
    let chunkSize = 100*1024*1024   

    timeIt $ print (readChunks chunkSize rest)

更新:我发现Chronograph包显示正确的执行时间(从此question获取的信息)。

1 个答案:

答案 0 :(得分:1)

嗯,你正在做一些没有定时的重要工作 - 这项工作弥补了差异似乎是合理的,即:

putStrLn name
content <- LBS.readFile name
let 
    (recNumStr, rest) = LBS.span (not.isControl) content
    recNum = LBS.readInt recNumStr
case recNum of
    Nothing -> putStrLn "can't parse"
    Just (value, rest) -> print (value)

如果你也是时间,那么你可能会发现最大的不同之处。另请注意,在您输入main之前还有其他操作(即使对于C程序也是如此)。