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 ...

Get Haskell Data Analysis Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.