34

I am relatively new to Haskell and I am trying to learn how different actions can be executed in sequence using the do notation. In particular, I am writing a program to benchmark an algorithm (a function)

foo :: [String] -> [String]

To this purpose I would like to write a function like

import System.CPUTime

benchmark :: [String] -> IO Integer
benchmark inputList = do
                         start <- getCPUTime
                         let r = foo inputList
                         end <- getCPUTime
                         return (end - start) -- Possible conversion needed.

The last line might need a conversion (e.g. to milliseconds) but this is not the topic of this question.

Is this the correct way to measure the time needed to compute function foo on some argument inputList?

In other words, will the expression foo inputList be completely reduced before the action end <- getCPUTime is executed? Or will r only be bound to the thunk foo inputList?

More in general, how can I ensure that an expression is completely evaluated before some action is executed?


This question was asked a few months ago on programmers (see here) and had an accepted answer there but it has been closed as off-topic because it belongs on stack overflow. The question could not be moved to stack overflow because it is older than 60 days. So, in agreement with the moderators, I am reposting the question here and posting the accepted question myself because I think it contains some useful information.

2
  • 4
    If you are just interested in benchmarking, you might want to check out the criterion library.
    – hugomg
    Jan 4, 2013 at 18:57
  • The foo function will not execute by the time you reach the final line. Haskell functions are only evaluated on demand, so you will need to do something with that r value before the assignment to end. Jan 4, 2013 at 21:32

2 Answers 2

22

Answer originally given by user ysdx on programmers:

Indeed you version will not benchmark your algorithm. As r is not used it will not be evaluated at all.

You should be able to do it with DeepSeq:

benchmark :: [String] -> IO Integer
benchmark inputList = do
                     start <- getCPUTime
                     let r = foo inputList
                     end <- r `deepseq` getCPUTime
                     return (end - start)

(a `deepseq` b) is some "magic" expression which forces the complete/recursive evaluation of a before returning b.

4
  • 2
    Usual way to save yourself from taking credit for others' work is to make the answer community wiki. Then the answer can still be upvoted/accepted, but you won't get the credit :) Jan 4, 2013 at 22:35
  • 2
    I did not know that, thanks. I have set the community wiki tag. Now the two upvoters can remove the upvote, if they happen to read this again.
    – Giorgio
    Jan 4, 2013 at 22:38
  • I still dont get it. What if I write let _ = a `deepseq` () instead? Will it force the evaluation?
    – Nawaz
    Jul 7, 2020 at 1:10
  • @Nawaz no. It only means that if a deepseq b is evaluated, then a will be fully evaluated before b is returned. In your example, it does not appear that the expression a deepseq () is evaluated.
    – jforberg
    Feb 14, 2021 at 14:21
14

I would use the language extension -XBangPatterns, I find that quite expressive in such situations. So you would have to say "let !r = foo inputList" as in:

{-# LANGUAGE BangPatterns #-}
import System.CPUTime

benchmark :: [String] -> IO Integer
benchmark inputList = do
                         start <- getCPUTime
                         let !r = foo inputList
                         end <- getCPUTime
                         return (end - start)
10
  • 3
    That would only evaluate the result to the outermost constructor, here, complete evaluation is desired. Jan 4, 2013 at 20:01
  • 2
    Do bang patterns ensure complete evaluation or will the expression be reduced to weak head normal form (stackoverflow.com/questions/6872898/…)?
    – Giorgio
    Jan 4, 2013 at 20:03
  • You can use BangPatterns in foo as well.
    – J Fritsch
    Jan 4, 2013 at 20:17
  • 2
    @Giorgio WHNF, bang patterns are a nicer seq. Jan 4, 2013 at 20:27
  • 1
    @Giorgio Even that is more than WHNF, <whatever> : <whatever else> is WHNF. You want complete evaluation, that is NF, so deepseq or ensuring that foo's result is completely evaluated when it is forced to WHNF. The latter is usually painful. Jan 4, 2013 at 20:41

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.