Haskell Criterion Usage Error

658 views Asked by At

The following code named fib.hs

import Criterion.Main (defaultMain)
fibZ = 1:1:zipWith (+) fibZ (tail fibZ)
main = defaultMain [
         bench "fibZ 10" $ \n -> fibZ (10+n-n)
       ]

errors with

fib.hs:45:10: Not in scope: `bench'

What is wrong? I have borrowed this example from here.

2

There are 2 answers

0
Jeremy W. Sherman On BEST ANSWER

The library has changed since that blog post was written. Now you should write:

 import Criterion.Main

 fib :: Int -> Int
 fib 0 = 0
 fib 1 = 1
 fib n = fib (n-1) + fib (n-2)

 main = defaultMain [
        bgroup "fib" [ bench "fib 10" $ B fib 10
                     , bench "fib 35" $ B fib 35
                     , bench "fib 37" $ B fib 37
                     ]
                    ]

This was taken directly from the "Running benchmarks" section of the Hackage documentation for Criterion.Main.

9
Mikhail Glushenkov On

Use

import Criterion.Main

instead of

import Criterion.Main (defaultMain)

The function bench from Criterion.Main is not in scope because you're importing only defaultMain. Using bgroup is not necessary.

Here's a full working example:

import Criterion.Main

fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)

main = defaultMain [
         bench "fib 10" $ nf fib 10
       , bench "fib 30" $ nf fib 30
       , bench "fib 35" $ nf fib 35
       ]

If you're wondering what these nf thingies are for, look at this section of the documentation.