3

我想测量 Haskell 执行某些功能和使用 TimeIt 包所花费的时间(我也尝试了这些建议)。但是显示的时间与应用程序花费的实际时间不同(我已经使用 +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包显示正确的执行时间(信息取自这个问题)。

4

1 回答 1

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 程序也是如此)。

于 2013-04-17T14:54:01.083 回答