1

I would like to compare two different functions to see who has more performance. Maybe there is an tool or ghci command?

  • 3
    [criterion](https://hackage.haskell.org/package/criterion) package is often used for this, also GHC has some support for this: [manual](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/profiling.html) – Michail Mar 31 '19 at 14:28
  • 3
    I find that duplicate link underwhelming. Do not benchmark in ghci. Never benchmark without using the optimizations you intend when running the code in production (compiling with -O2 presumably). I suggest using criterion such as shown here https://stackoverflow.com/questions/4033103/type-error-using-criterion/4034701#4034701. – Thomas M. DuBuisson Mar 31 '19 at 17:34

1 Answers1

3

Run :set +s in GHCi. You'll then get time and memory allocation after each evaluation, like this:

GHCi, version 8.6.3: http://www.haskell.org/ghc/  :? for help
Prelude> :set +s
Prelude> product (replicate 10000000 1)
1
(1.78 secs, 1,292,363,104 bytes)
Prelude>
  • 6
    Note however that GHCi disables a lot of optimizations. For serious benchmarks, one really need to compile with `-O` and run the executable. For `print (product (replicate 10000000 (1 :: Int)))`, I get 2.44s in GHCi and 0.13s after compiling. – chi Mar 31 '19 at 14:52
  • 4
    @chi ...and with `-O2 -fllvm` it becomes 0.00s! – Daniel Wagner Mar 31 '19 at 14:59
  • 2
    I strongly discourage anyone consider performance when using GHCi. When you are concerned about performance be sure you compile the code, use optimizations, and preferably use a benchmark package like criterion. – Thomas M. DuBuisson Mar 31 '19 at 17:35
  • 1
    Will some types of things be significantly slower/faster relative to GHCi? (Eg. use of ffi is 5x faster, code with lots of newtypes 10x faster, everything else 20x faster) @Thomas – moonGoose Mar 31 '19 at 20:50