2012-11-14 26 views
1

我正在执行ssh命令readProcess(来自System.Process包),我想花时间执行多长时间并将其与print命令输出。寻找一个简单的方法来计时执行readProcess(没有标准)

我知道可以根据标准准确计时,但标准似乎花时间进行大量设置和拆卸以及打印额外的输出。我不打算运行完整的基准测试,只是显示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。用!注释似乎没有区别。

谢谢!

回答

4

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

而不是getCPUTime,你希望在你想要的时间之前和之后得到挂钟时间。在Data.Time.Clock中试试getCurrentTime

+0

谢谢!这就说得通了。 '''getCurrentTime'''给出了期望的结果。 – jhickner

相关问题