module System.TimeIt(timeIt, timeItT) where
import System.CPUTime
import Text.Printf

-- |Wrap an 'IO' computation so that it prints out the execution time.
timeIt :: IO a -> IO a
timeIt :: IO a -> IO a
timeIt ioa :: IO a
ioa = do
    (t :: Double
t, a :: a
a) <- IO a -> IO (Double, a)
forall a. IO a -> IO (Double, a)
timeItT IO a
ioa
    String -> Double -> IO ()
forall r. PrintfType r => String -> r
printf "CPU time: %6.2fs\n" Double
t
    a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
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 :: IO a -> IO (Double, a)
timeItT ioa :: IO a
ioa = do
    Integer
t1 <- IO Integer
getCPUTime
    a
a <- IO a
ioa
    Integer
t2 <- IO Integer
getCPUTime
    let t :: Double
	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
* 1e-12
    (Double, a) -> IO (Double, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Double
t, a
a)