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 ./TimingTestWe must get something like this:
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 timeand
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:
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 severeIf you'll have any questions/suggestions write me in comments or ping me at twitter: @0xAX.