[Haskell-cafe] call for a better timing method
jiansenhe at googlemail.com
Thu Nov 4 15:09:57 EDT 2010
I want to write a timing method to test sub steps in my program. I tested
my timing method as follows:
time :: String -> IO a -> IO a
time str a = do
start <- getCurrentTime
v <- a
end <- getCurrentTime
let diff = diffUTCTime end start
putStrLn $ str ++ showNominalDiffTime (diff)
showNominalDiffTime :: NominalDiffTime -> String
showNominalDiffTime nTime = let timeString = show nTime in
take (length timeString - 1) timeString ++ "
fun :: IO (Int, [Int])
fun = do
i <- time "product: " $ return $! product [1..10000] -- testing
strictness : evaluated
j <- time "product list: " $ return $! map (\_ -> product [1..10000])
[1..3] -- testing strictness : not evaluated List is in hnf
return (i, j)
main = fun
In the shell
$ time ./time
product: 0.001813 sec
product list: 0.000001 sec
Notice that the ($!) operation will reduce a term to head normal form (hnf),
not normal form.
In the "product list" testing, the "complicated" map evaluation is reduced
to a list of redex. Therefore, the result does not reflect the time needed
to get the final value.
How could I improve my timing method so that it will give the time to reduce
a general expression to normal form?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe