Book Image

Haskell Data Analysis Cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis Cookbook

By: Nishant Shukla

Overview of this book

Table of Contents (19 chapters)
Haskell Data Analysis Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Using the criterion package to measure performance


For more reliable performance measures, the criterion package comes in handy. The package description points out a major flaw in using simple procedures to time pure code.

"Because GHC optimizes aggressively when compiling with -O, it is potentially easy to write innocent-looking benchmark code that will only be evaluated once, for which all but the first iteration of the timing loop will be timing the cost of doing nothing."

Getting ready

Create a small.txt file with a few words. Create a file, big.txt, filled with text as follows:

$ wget norvig.com/big.txt

Install the criterion library as follows:

$ cabal install criterion

How to do it…

  1. Import the package as follows:

    import Criterion.Main
  2. Define the I/O function we wish to time as follows:

    splitUp filename = readFile filename >>= return . words
  3. Benchmark the desired function as follows:

    main = defaultMain 
      [ bgroup "splitUp" 
        [ bench "big" $ nfIO $ splitUp "big.txt" 
        , bench "small...