Time in Haskell

Find this useful? Support us: Star on GitHub 6
Category: Other | Language: Haskell

In Haskell, we can use the time package to measure the execution time of our programs. Here's how to use it:

1. Import the Data.Time module:

import Data.Time

2. Define a function that you want to measure the execution time of:

myFunction :: Int -> Int
myFunction n = sum [1..n]

3. Wrap your function inside the getCurrentTime and diffUTCTime functions. This will measure the execution time of your function:

main = do
  start <- getCurrentTime
  let result = myFunction 10000000
  end <- getCurrentTime
  putStrLn $ "Result: " ++ show result
  putStrLn $ "Execution time: " ++ show (diffUTCTime end start)

In this example, we're measuring the execution time of myFunction with an input of 10000000. The getCurrentTime function gets the current time before and after the function is executed, and diffUTCTime calculates the difference between the two times. The result will be in picoseconds.

Here's an example of what the output might look like:

Result: 50000005000000
Execution time: 0.052981s

This tells us that myFunction took approximately 0.052981 seconds to execute.