【发布时间】:2012-11-14 00:21:55
【问题描述】:
我正在使用 readProcess(来自 System.Process 包)执行 ssh 命令,我想计算执行所需的时间并将其与命令输出一起打印出来。
我知道可以使用标准准确计时,但标准似乎花费时间进行大量设置和拆卸以及打印额外的输出。我不想运行完整的基准测试,只是显示 ssh 命令运行的时间。
到目前为止,我已经尝试了System.TimeIt 包,但结果不正确。例如,这应该报告至少 5 秒(仅运行 sleep 5):
>>> timeIt $ readProcess "sleep" ["5"] []
CPU time: 0.04s
这是System.TimeIt 在幕后所做的:
-- |Wrap an 'IO' computation so that it prints out the execution time.
timeIt :: IO a -> IO a
timeIt ioa = do
(t, a) <- timeItT ioa
printf "CPU time: %6.2fs\n" t
return a
-- |Wrap an 'IO' computation so that it returns execution time is seconds as well as the real value.
timeItT :: IO a -> IO (Double, a)
timeItT ioa = do
t1 <- getCPUTime
a <- ioa
t2 <- getCPUTime
let t :: Double
t = fromIntegral (t2-t1) * 1e-12
return (t, a)
看起来很简单,但我想不出一种方法来强制执行a <- ioa。用!注解似乎没什么区别。
谢谢!
【问题讨论】:
标签: haskell