chronograph-0.1.0.0: measure timings of data evaluation

Safe HaskellNone

Data.Chronograph

Contents

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

  import Text.Printf

  formatOutput :: FilePath -> Chronograph Int -> String
  formatOutput fp chr = printf "%s :: %d, %s" fp (val chr) (show $ measure chr)

  procFile :: FilePath -> IO ()
  procFile fp = do
      doc <- readFile fp
      let wc = length $ lines doc
      putStrLn $ formatOutput fp (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
    putStrLn $ formatOutput fp wc
  main :: IO ()
  main = do
      args <- getArgs
      putStrLn "pure Chronograph"
      mapM_ procFile args
      putStrLn "IO Chronograph"
      mapM_ procIO args

Synopsis

Documentation

data Chronograph a Source

Constructors

Chronograph 

Fields

val :: a
 
measure :: NominalDiffTime
 

Instances

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.