-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTiming.hs
More file actions
35 lines (27 loc) · 969 Bytes
/
Timing.hs
File metadata and controls
35 lines (27 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Timing ( getTick
, timeIt
) where
import Control.Exception
import Data.Time.Clock
import Control.Monad.IO.Class
import System.IO.Unsafe
-- Timing functions
-- TODO: Consider just using the criterion package for all performance measurements
-- http://hackage.haskell.org/package/criterion
{-# NOINLINE startTime #-}
startTime :: UTCTime
startTime = unsafePerformIO getCurrentTime
-- In seconds
getTick :: IO Double
getTick = do
-- Make sure startTime has been evaluated, otherwise the getCurrentTime in the
-- unsafePerformIO might be evaluated after the getCurrentTime here, returning a
-- negative tick on the first call to getTick
st <- evaluate startTime
(realToFrac . flip diffUTCTime st) <$> getCurrentTime
timeIt :: MonadIO m => m a -> m (Double, a)
timeIt f = do
start <- liftIO getCurrentTime
r <- f
end <- liftIO getCurrentTime
return (realToFrac $ diffUTCTime end start, r)