| Safe Haskell | None |
|---|
Data.Chronograph
Description
Measure data and IO evaluation time in a lightweight manner.
A 'Chronograph a' has two parts, the value a and the measurement of
evaluation time. A Chronograph is lazy, so a is only evaluted on demand.
This example counts the lines in a number of files, and records the evaluation time taken for each one.
import System.Environment
import Control.Applicative
import Data.Chronograph
procFile :: FilePath -> IO Int
procFile fp = do
doc <- readFile fp
let wc = length $ lines doc
chronoPrint "time to eval length" (chrono wc)
chrono creates a chronograph that evaluates its input as far as seq would.
In this case the input wc is an Int, so chrono fully evaluates it.
deepseq-style evaluation is performed by chronoNF, and custom evaluation
strategies can be implemented with chronoBy.
although wc is a pure value, IO is lazily performed in its evalution.
This IO cost is included in chronos measurement.
You can explicitly include timings of IO actions as well:
fileLinesIO :: FilePath -> IO Int
fileLinesIO fp = length . lines <$> readFile fp
procIO :: FilePath -> IO ()
procIO fp = do
wc <- chronoIO $ fileLinesIO fp
void $ chronoPrint "fileLinesIO" wc
main :: IO ()
main = do
args <- getArgs
putStrLn "pure Chronograph"
mapM_ procFile args
putStrLn "IO Chronograph"
mapM_ procIO args
- data Chronograph a = Chronograph {
- measure :: !NominalDiffTime
- val :: a
- chrono :: a -> Chronograph a
- chronoNF :: NFData a => a -> Chronograph a
- chronoBy :: (a -> ()) -> a -> Chronograph a
- chronoJustIO :: IO a -> IO (Chronograph a)
- chronoIO :: IO a -> IO (Chronograph a)
- chronoNFIO :: NFData a => IO a -> IO (Chronograph a)
- chronoIOBy :: (a -> ()) -> IO a -> IO (Chronograph a)
- chronoPrint :: String -> Chronograph a -> IO a
- chronoTraceEvent :: String -> Chronograph a -> a
- chronoTraceEventIO :: String -> Chronograph a -> IO a
Documentation
data Chronograph a Source
Constructors
| Chronograph | |
Fields
| |
Instances
| Show a => Show (Chronograph a) | |
| Generic (Chronograph a) |
chrono pure stuff
chrono :: a -> Chronograph aSource
Add a Chronograph to measure evaluation to weak head normal form.
chronoNF :: NFData a => a -> Chronograph aSource
Add a Chronograph to measure evaluation to normal form.
chronoBy :: (a -> ()) -> a -> Chronograph aSource
Add a Chronograph to measure evalution time with the provided strategy.
chrono IO stuff
chronoJustIO :: IO a -> IO (Chronograph a)Source
Add a Chronograph to measure IO time (no additional evaluation is
performed, although the IO action itself may perform some evaluation)
chronoIO :: IO a -> IO (Chronograph a)Source
Add a Chronograph to measure time of IO and evaluation to weak head
normal form.
chronoNFIO :: NFData a => IO a -> IO (Chronograph a)Source
Add a Chronograph to measure time of IO and evaluation to normal form.
chronoIOBy :: (a -> ()) -> IO a -> IO (Chronograph a)Source
Add a Chronograph to measure time of IO and evaluation with the
provided strategy.
utility functions
chronoPrint :: String -> Chronograph a -> IO aSource
print the measure to stdout and return the value
chronoTraceEvent :: String -> Chronograph a -> aSource
write the measure to the ghc eventlog and return the value
chronoTraceEventIO :: String -> Chronograph a -> IO aSource
write the measure to the ghc eventlog and return the value in IO