How to get a program's running time in Haskell

user645466 picture user645466 · May 11, 2011 · Viewed 8.8k times · Source

How can I go about getting a program's running time through system time functions in Haskell? I would like to measure the execution time of a whole program and/or an individual function.

Answer

Thomas M. DuBuisson picture Thomas M. DuBuisson · May 11, 2011

1) If you want to benchmark something, use the criterion package.

2) If you want to time a function and are positive you have controlled for laziness as needed, then just use Data.Time.getCurrentTime from the time package.:

import Data.Time
...
   start <- getCurrentTime
   runOperation
   stop <- getCurrentTime
   print $ diffUTCTime stop start

A slicker packaging of the above pattern can be found in the timeit package.

3) If you actually want the running time of a program that just happens to be written in Haskell then use your systems time utility. For most POSIX systems (Mac, Linux) just run:

$ time ./SomeProgram

And it will report user, wall, and system time.