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

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.