haskellhaskell-criterion

Haskell criterion with two inputs


I want to benchmark a function with two inputs using criterion. However, it seems the function for benchmarking from criterion can only take one input. I'm quite new to Haskell, and I'm sure there exists answers here on SO already for this question, but I cannot find them.

My setup is something like this, where benchmark is called with an Int determining which bgroup to test. I've tried to make the example as minimal as possible.

import Criterion.Main ( bench, bgroup, whnf, defaultMain, nf )

benchmark :: Int -> IO ()
benchmark 0 = defaultMain [
    bgroup "group1" [bench "test1" $ whnf fun l1 l2
                    ]]
main :: IO ()
main = do
    benchmark 0

Relevant declarations are

fun :: [Int] -> [Double] -> [[Double]]
l1  :: [Int]
l2  :: [Double]

If I omit l2 the program can run, but I will have run times in the nanoseconds, while I know this function takes about a minute for one iteration. I though updating the call to whnf fun $ l1 l2 would be okay, but it wasn't.

How can I update the call to fun so that I may pass two variables?


Solution

  • I looked at the Criterion documentation and it says:

    The second will cause results to be evaluated to weak head normal form (the Haskell default):

    whnf :: (a -> b) -> a -> Benchmarkable
    

    As both of these types suggest, when you want to benchmark a function, you must supply two values:

    • The first element is the function, saturated with all but its last argument.
    • The second element is the last argument to the function.

    So:

    bench "test1" $ whnf (fun l1) l2