Book Image

Haskell Data Analysis cookbook

By : Nishant Shukla
Book Image

Haskell Data Analysis cookbook

By: Nishant Shukla

Overview of this book

Step-by-step recipes filled with practical code samples and engaging examples demonstrate Haskell in practice, and then the concepts behind the code. This book shows functional developers and analysts how to leverage their existing knowledge of Haskell specifically for high-quality data analysis. A good understanding of data sets and functional programming is assumed.
Table of Contents (14 chapters)
13
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...