Search code examples
haskellghctimingghci

Get evaluation timings in GHCi


I have a relatively slow procedure (aptly named slow), and I would like to do something like

time $ slow [1,2,3,4,5]

in the console (REPL) to get the time, instead of having to compile the program and then run time.

Can this be done?


Solution

  • If you enter :set +s in GHCi, then timing and memory info will be printed after the evaluation of every expression.

    Example:

    Prelude> :set +s
    Prelude> sum [1..2^20]
    549756338176
    it :: (Num a, Enum a) => a
    (0.34 secs, 169,197,008 bytes)
    

    Note that this will be the timing of the expression as evaluated in the interpreter, without optimisation, so it won't necessarily be an accurate measure of how long things take, or even which of two versions of the same code will be faster, in actual compiled code. For that, take a look at the criterion benchmarking library.