1

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.

hammar
  • 138,522
  • 17
  • 304
  • 385
Nordlöw
  • 11,838
  • 10
  • 52
  • 99

2 Answers2

8

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.

Mikhail Glushenkov
  • 14,928
  • 3
  • 52
  • 65
  • Your code errors for me as `fib.hs:53:37: Not in scope: data constructor `B' Failed, modules loaded: none.` Sorry but I'm really new to Haskell. – Nordlöw Oct 19 '11 at 22:57
  • 1
    You are probably talking about Jeremy's code - he copy-pasted his example from the doc page for an older version of criterion. Mine compiles without errors. – Mikhail Glushenkov Oct 19 '11 at 23:01
  • Sigh, now the code loads but instead I get an error when I call main: `Loading package double-conversion-0.2.0.1 ... can't load .so/.DLL for: stdc++ (libstdc++.so: cannot open shared object file: No such file or directory)`. I'm using GHC 7.0.4 on Ubuntu 11.10. – Nordlöw Oct 19 '11 at 23:26
  • I think it's a known bug - fortunately, this happens only when you try to run your program in GHCi. Try compiling (with `ghc --make -O2`). – Mikhail Glushenkov Oct 19 '11 at 23:28
  • Now it runs but it only supports output type CSV. All other including PNG, PDF, SVG and win fails with an error like `ERROR: output type PDF 432 324 not supported on this platform`. Any clues? – Nordlöw Oct 19 '11 at 23:40
  • These [are not enabled by default](http://hackage.haskell.org/packages/archive/criterion/0.5.1.0/criterion.cabal). Try `cabal install --reinstall criterion -fChart -fgtk` This will recompile criterion with support for drawing charts and displaying graphs in a gtk window. – Mikhail Glushenkov Oct 19 '11 at 23:46
  • Ok, when I reinstall criterion I get `setup: The program gtk2hsC2hs version >=0.13.5 is required but the version found at /usr/bin/gtk2hsC2hs is version 0.13.4`. Do I have to wait for GHC 7.2.1? – Nordlöw Oct 19 '11 at 23:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/4403/discussion-between-nordlow-and-mikhail-glushenkov) – Nordlöw Oct 19 '11 at 23:57
  • 1
    My inital guess was right - you have an old version of gtk2hs-buildtools, and since [build tools are not considered dependencies by `cabal-install`](http://hackage.haskell.org/trac/hackage/ticket/227), you should update them manually. Do `cabal install gtk2hs-buildtools` and then `cabal install --reinstall criterion -fChart -fgtk`. – Mikhail Glushenkov Oct 20 '11 at 00:24
3

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.

Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111