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…
- Import the necessary libraries as follows:
import System.CPUTime (getCPUTime) import Control.Monad (replicateM_) import Control.Parallel.Strategies (NFData, rdeepseq) import Control.Exception (evaluate)
- 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_...