    module System.TimeIt(timeIt, timeItShow, timeItNamed, timeItT) where
    import System.CPUTime
    import Text.Printf
    import Control.Monad.IO.Class (MonadIO(liftIO))

    -- | Wrap a 'MonadIO' computation so that it prints out the execution time.
    timeIt :: MonadIO m => m a -> m a
    timeIt :: forall (m :: * -> *) a. MonadIO m => m a -> m a
    timeIt = String -> m a -> m a
    forall (m :: * -> *) a. MonadIO m => String -> m a -> m a
    timeItNamed String
    "CPU time"

    -- | Like 'timeIt', but uses the 'show' rendering of @a@ as label for the
    -- timing.
    --
    -- @since 2.0
    timeItShow :: (MonadIO m, Show a) => m a -> m a
    timeItShow :: forall (m :: * -> *) a. (MonadIO m, Show a) => m a -> m a
    timeItShow m a
    ioa = do
        (t, a) <- m a -> m (Double, a)
    forall (m :: * -> *) a. MonadIO m => m a -> m (Double, a)
    timeItT m a
    ioa
        liftIO $ printf (show a ++ ": %6.2fs\n") t
        return a

    -- | Like 'timeIt', but uses the 'String' as label for the timing.
    --
    -- @since 2.0
    timeItNamed :: MonadIO m => String -> m a -> m a
    timeItNamed :: forall (m :: * -> *) a. MonadIO m => String -> m a -> m a
    timeItNamed String
    name m a
    ioa = do
        (t, a) <- m a -> m (Double, a)
    forall (m :: * -> *) a. MonadIO m => m a -> m (Double, a)
    timeItT m a
    ioa
        liftIO $ printf (name ++ ": %6.2fs\n") t
        return a

    -- | Wrap a 'MonadIO' computation so that it returns execution time in seconds,
    -- as well as the result value.
    timeItT :: MonadIO m => m a -> m (Double, a)
    timeItT :: forall (m :: * -> *) a. MonadIO m => m a -> m (Double, a)
    timeItT m a
    ioa = do
        t1 <- IO Integer -> m Integer
    forall a. IO a -> m a
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
    liftIO IO Integer
    getCPUTime
        a <- ioa
        t2 <- liftIO getCPUTime
        let t :: Double
            t = Integer -> Double
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral (Integer
    t2Integer -> Integer -> Integer
    forall a. Num a => a -> a -> a
    -Integer
    t1) Double -> Double -> Double
    forall a. Num a => a -> a -> a
    * Double
    1e-12
        return (t, a)