32

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?

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
Christian Neverdal
  • 5,655
  • 6
  • 38
  • 93
  • 4
    In ghci? `:set +s` gets you timing and allocation stats for all evaluated expressions. You can write a `time` yourself using `System.CPUTime.getCPUTime`, if you want. – Daniel Fischer Apr 26 '12 at 22:23
  • 1
    Just note that the interactive program will not give you accurate information about compiled performance. – rotskoff Apr 26 '12 at 23:02
  • 6
    You might like `time runhaskell foo.hs` and its more honest fellow `ghc foo.hs -O2 && time ./foo`. – Daniel Wagner Apr 27 '12 at 00:27

1 Answers1

51

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.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
ehird
  • 40,602
  • 3
  • 180
  • 182