Get function execution time in Haskell

Some times ago i encountered with question: How to get function execution time in Haskell program? I asked this question at StackOverflow, and got some useful answers. Here i will try to describe how to do it.

For example we have simple haskell program which will calculate sum of prime numbers which are between 0 and 10000. Something like this:

Yes. It's not the best implementation of prime numbers, but it's not important at the current moment. Let's see what we have for checking execution time.


First of all, the simplest method to get execution time is time command. Compile our source code and execute:
time ./TimingTest
We must get something like this:
real 0m3.503s
user 0m3.492s
sys 0m0.004s


Second method is the simplest things is to just add :set +s in ghci before function execution. Of course it's not the best method, because functions run much slower in ghci.


In third method we will use TimeIt library by Lennart Augustsson. Very little, but useful library with simple API. It consist only from two functions:

timeIt :: IO a -> IO a -- | Wrap an IO computation so that it prints out the execution time
timeItT :: IO a -> IO (Double, a)Source -- | Wrap an IO computation so that it returns execution time is seconds as well as the real value.

Will remake our main function as:

and will get something like this:
Result: 5736396
CPU time: 8.22s


Criterion - is a library provides a powerful but simple way to measure software performance by Bryan O'Sullivan. For using it, will remake again our main function as:

after our program running we got:
estimating clock resolution...
mean is 3.760062 us (160001 iterations)
found 3006 outliers among 159999 samples (1.9%)
    2461 (1.5%) high severe
estimating cost of a clock call...
mean is 98.08812 ns (28 iterations)
found 4 outliers among 28 samples (14.3%)
    4 (14.3%) low severe
If you'll have any questions/suggestions write me in comments or ping me at twitter: @0xAX.


Anonymous said…
Why does timeIt 2x slower than just using time or criterion? Did you use different input, or machine?
0xax said…
It's interesting moment :) I used the same machine for the all tests... Trying to understand why this matters myself.