我正在使用readProcess
(从System.Process
包)执行ssh命令,我想要计时执行所需的时间,并将其与命令输出一起打印。
我知道使用criterion来精确计时是可能的,但criterion似乎花费了大量的时间进行设置和拆卸,以及打印额外的输出。我并不打算运行一个完整的基准测试,只是显示运行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
的方法。使用!
进行注释似乎没有什么不同。
谢谢!
发布于 2012-11-14 08:31:18
这里的问题不是延迟计算,而是sleep
不会在计算上浪费任何CPU时间,它只是让程序在给定的延迟期间暂停执行。因此,TimeIt
正确地报告了IO操作花费在计算上的时间量。
您希望在要计时的操作之前和之后获得挂钟时间,而不是getCPUTime
。在Data.Time.Clock
中尝试使用getCurrentTime
。
https://stackoverflow.com/questions/13371207
复制相似问题