0 | module TimeIt
 1 |
 2 | import public System.Clock
 3 |
 4 | {- Deadass IO timing
 5 |
 6 | :exec timeIt "Woof" $ traverse (\n => sleep n <* printLn n) [3,1,2]
 7 | 3
 8 | 1
 9 | 2
10 | Woof: 6.000343895
11 |
12 | -}
13 |
14 | -- showTime doesn't pad nanoseconds properly
15 | export
16 | showTime' : Clock Duration -> String
17 | showTime' t = show $ (cast {to=Double} (seconds t)) + (cast {to=Double} (nanoseconds t) / 1000000000)
18 |
19 | export
20 | timeIt' : HasIO io => String -> io a -> io (Clock Duration,a)
21 | timeIt' str act = do
22 |   now <- liftIO $ clockTime Monotonic
23 |   r <- act
24 |   later <- liftIO $ clockTime Monotonic
25 |   let dif = timeDifference later now
26 |   putStrLn $ str ++ ": " ++ showTime' dif
27 |   pure (dif,r)
28 |
29 | -- repeated definition since calling the above and dropping the Clock affects timing somehow!
30 | export
31 | timeIt : HasIO io => String -> io a -> io a
32 | timeIt str act = do
33 |   now <- liftIO $ clockTime Monotonic
34 |   r <- act
35 |   later <- liftIO $ clockTime Monotonic
36 |   let dif = timeDifference later now
37 |   putStrLn $ str ++ ": " ++ showTime' dif
38 |   pure r
39 |