Microbenchmarking can be used to compare the speed of different approaches to the same operation. Since most code is very fast, to get accurate timing information you must run the operation many times and then divide to get the time per operation.
This library manages the microbenchmarking process: it finds how many iterations of a function are needed to get a good timing estimate per iteration and prints out a human-readable "Your code takes n nanoseconds to run, and can run n times per second".
The only function microbench
takes a function that expects an
integer parameter (which is the quantity you're trying to measure),
and probes the function with increasing parameters until enough time
has elapsed to get a good measurement.
This may be better understood by some example code:
sum1 n = sum [1..n] sum2 n = foldl (+) 0 [1..n] main = do microbench "Sum using sum" sum1 microbench "Sum using foldl" sum2
When run, sum1
and sum2
are called with varying values of n
.
The output, then, is an estimate of how many integers these
approaches could sum per second.
microbench
also accepts a parameter of type IO ()
for
benchmarking. It does the same probing process, but manages running
the operation in a loop.
- microbench :: Microbenchable a => String -> a -> IO ()
- class Microbenchable a
Documentation
microbench :: Microbenchable a => String -> a -> IO ()Source
microbench description target
probes target with different parameters
until it's ran enough iterations to have a good estimate at the rate per
second of the operation. description
is a textual description of the
thing being benchmarked. Outputs to stdout.
class Microbenchable a Source
Microbenchmarkable computations. Be very wary of adding your own instances of this class, as it's difficult to force GHC to re-evaluate code in a way that makes benchmarking easy.
Microbenchable (IO ()) | |
Microbenchable (Int -> IO ()) | |
Microbenchable (Int -> a) |