I have several functions in main that output information.
I need to measure their execution time.
Currently it’s done like this:
start <- getCurrentTime
putStrLn $ show $ findNthMinLinear (getTestArrayOfLength (read arrlength :: Int)) 4
stop <- getCurrentTime
print $ diffUTCTime stop start
I want to wrap each function in a function that 1. remembers the time 2. evaluates function 3. remembers the time 4. prints difference between two remembered times
The problem is that i don’t know how to pass a function without evaluating it first.
My guess is that if i write (<5) for example, that is i create a function that returns a function, no real work will be done until i pass final parameter i guess?
Then i could write
printAndMeasure :: (a -> IO()) -> IO ()
...
printAndMeasure \x -> (getTestArrayOfLength (read arrlength :: Int)) 4
but it’s a sloppy solution because i don’t really need this x other than to hold function from being executed.
Passing a function does not evaluate it. So you can have function like
IO () -> IO ()and passIOactions to them. When a program is run onlymainfunction is executed and arguments of a function are not evaluated until needed (laziness).If you are trying to benchmark your code then it is better to use benchmarking libraries like
Criterion.