【问题标题】:Looking for a simple way to time the execution of readProcess (without criterion)寻找一种简单的方法来计时 readProcess 的执行(没有标准)
【发布时间】: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 &lt;- ioa。用!注解似乎没什么区别。

谢谢!

【问题讨论】:

    标签: haskell


    【解决方案1】:

    你的问题不是懒惰的评估——而是sleep 不会在计算上浪费任何 CPU 时间,它只是让程序在给定的延迟期间暂停执行。所以TimeIt 正确地报告了您的 IO 操作花费在计算上的时间。

    而不是getCPUTime,您想要在您想要计时的操作之前和之后获取挂钟时间。在Data.Time.Clock 中尝试getCurrentTime

    【讨论】:

    • 谢谢!这就说得通了。 getCurrentTime 给出了想要的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2011-01-21
    • 2013-11-16
    • 2018-01-27
    相关资源
    最近更新 更多