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

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_ trials $...