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

Benchmarking runtime performance in Haskell

Benchmarking runtime is the process of timing how long it takes for the code to run. We can understand whether our parallel or concurrent code is in fact faster than the naive implementation by proper benchmarking. This recipe will demonstrate how to time code runtime in Haskell.

How to do it…

  1. Import the necessary libraries as follows:
    import System.CPUTime (getCPUTime)
    import Control.Monad (replicateM_)
    import Control.Parallel.Strategies (NFData, rdeepseq)
    import Control.Exception (evaluate)
  2. Create a function to print out the duration of a pure task. Evaluate the pure expression a very large number of times (10^6), and then calculate the average CPU time it takes to run one pure task. The getCPUTime function returns the number of picoseconds since the start of the program's execution, as shown in the following code snippet:
    time :: (Num t, NFData t) => t -> IO ()
    time y = do
      let trials = 10^6
      start <- getCPUTime
      replicateM_...