Although so.com is no site for recommendations I advise you to take a look at criterion https://hackage.haskell.org/package/criterion
I'll maybe give some example of it's usage tomorrow
If you really want to dive deep in that matter you can analyze the generated llvm assembler by adding the compiler option --ddump-llvm
though that is a rather advanced topic only included for the sake of completeness.
Update - How to use criterion
in this case
First of all I will explain this using the haskell stack tool, all of the code can be found at github/epsilonhalbe
First of all we create a project and split each of the relevant definitions in a separate module (otherwise we would need data Tree
, data Tree'
and data Tree''
). See Chi.hs
as an example:
module Chi where
data Tree a = Empty | Node a [Tree a] deriving (Eq, Show)
addNums :: (Num a) => Tree a -> a
addNums Empty = 0
addNums (Node n xs) = n + sum (map addNums xs)
myInts :: Tree Int
myInts =
Node 1 [
Node 2 [
Node 4 [Empty], Node 5 [Empty]
],
Node 3 [
Node 6 [Empty], Node 7 [Empty], Node 8 [Empty]
]
]
myDouble :: Tree Double
myDouble =
Node 1 [
Node 2 [
Node 4 [Empty], Node 5 [Empty]
],
Node 3 [
Node 6 [Empty], Node 7 [Empty], Node 8 [Empty]
]
]
Note: that for User3237465.hs
we need a Language Pragma
{-# LANGUAGE DeriveFoldable #-}
module User3237465 where
data Tree a = Empty | Node a [Tree a] deriving (Eq, Show, Foldable)
addNums :: Num a => Tree a -> a
addNums = sum
myInts ..
myDouble ..
We build a Folder/File structure like the following ( this we get with stack new critExample
and a bit of copying/renaming/deleting)
../haskell/critExample/
▾ src/
Chi.hs
Sibi.hs
User3237465.hs
▾ bench/
Benchmarks.hs
critExample.cabal
LICENSE
Setup.hs
stack.yaml
the contents of critExample.cabal
also needs some adjustment,
name: critExample
[... non-important stuff ...]
library
hs-source-dirs: src
-- don't forget to adjust the exposed modules
exposed-modules: Chi
, Sibi
, User3237465
build-depends: base >= 4.7 && < 5
default-language: Haskell2010
-- and add the following benchmark part
benchmark addNums
type: exitcode-stdio-1.0
hs-source-dirs: bench
main-is: Benchmarks.hs
build-depends: base
, critExample
, criterion
default-language: Haskell2010
[...]
then we can begin to write our benchmarks
Benchmarks.hs
module Main where
import Criterion
import Criterion.Main
import qualified Chi
import qualified Sibi
import qualified User3237465
main :: IO ()
main = defaultMain [
bgroup "myInts" [ bench "Sibi" $ whnf Sibi.addNums Sibi.myInts
, bench "Chi" $ whnf Chi.addNums Chi.myInts
, bench "User3237465" $ whnf User3237465.addNums User3237465.myInts
],
bgroup "myDouble" [ bench "Sibi" $ whnf Sibi.addNums Sibi.myDouble
, bench "Chi" $ whnf Chi.addNums Chi.myDouble
, bench "User3237465" $ whnf User3237465.addNums User3237465.myDouble ]
]
Note that whnf
only evaluates to weak head normal form, i.e. to the first constructor it sees - for a list this would be after the first element when it sees the (:)
operator for tuples it wouldn't evaluate a thing, but for Int
or Double
it fully evaluates stuff. If you need 'deep' evaluation use nf
instead of whnf
- if you are not sure what is needed, try both whnf
is usually unreasonably fast (like nanoseconds for ultra-long lists - as it only checks the head of that list).
You can build the project with stack build
and then invoke the benchmarks with stack bench
(triggers all available benchmarks) or stack bench critExample:addNums
(useful if you have more than one benchmark suites and only want to run a specific one), usage is always projectname:name of benchmarks given in cabal-file
.
If you want fancy html output (- and believe me you want it, because bryan o'sullivan put a lot of effort in it to make it sexy) you'll have to:
./.stack-work/dist/x86_64-linux/Cabal-1.22.4.0/build/addNums/addNums --output index.html
of course this path might vary if you do not use a linux operating system.
Update2
The results of the benchmarks - I don't know how representative they are - I ran them in a virtualized linux!
Running 1 benchmarks...
Benchmark addNums: RUNNING...
benchmarking myInts/Sibi
time 616.7 ns (614.1 ns .. 619.2 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 619.1 ns (615.4 ns .. 626.8 ns)
std dev 17.09 ns (9.625 ns .. 31.62 ns)
variance introduced by outliers: 38% (moderately inflated)
benchmarking myInts/Chi
time 582.6 ns (576.5 ns .. 592.1 ns)
0.998 R² (0.996 R² .. 1.000 R²)
mean 586.2 ns (581.5 ns .. 595.5 ns)
std dev 21.14 ns (11.56 ns .. 33.61 ns)
variance introduced by outliers: 52% (severely inflated)
benchmarking myInts/User3237465
time 606.5 ns (604.9 ns .. 608.2 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 607.0 ns (605.5 ns .. 609.2 ns)
std dev 5.915 ns (3.992 ns .. 9.798 ns)
benchmarking myInts/User3237465 -- folding variant see comments
time 371.0 ns (370.2 ns .. 371.7 ns)
1.000 R² (1.000 R² .. 1.000 R²)
mean 372.5 ns (370.8 ns .. 375.0 ns)
std dev 6.824 ns (4.076 ns .. 11.19 ns)
variance introduced by outliers: 22% (moderately inflated)
benchmarking myDouble/Sibi
time 678.9 ns (642.3 ns .. 743.8 ns)
0.978 R² (0.958 R² .. 1.000 R²)
mean 649.9 ns (641.1 ns .. 681.6 ns)
std dev 50.99 ns (12.60 ns .. 105.0 ns)
variance introduced by outliers: 84% (severely inflated)
benchmarking myDouble/Chi
time 643.3 ns (617.4 ns .. 673.6 ns)
0.987 R² (0.979 R² .. 0.996 R²)
mean 640.6 ns (626.7 ns .. 665.6 ns)
std dev 58.35 ns (40.63 ns .. 87.82 ns)
variance introduced by outliers: 88% (severely inflated)
benchmarking myDouble/User3237465
time 630.4 ns (622.9 ns .. 638.5 ns)
0.997 R² (0.994 R² .. 0.999 R²)
mean 637.8 ns (625.4 ns .. 659.8 ns)
std dev 53.15 ns (33.46 ns .. 78.36 ns)
variance introduced by outliers: 85% (severely inflated)
benchmarking myDouble/User3237465 -- folding variant see comments
time 398.1 ns (380.7 ns .. 422.0 ns)
0.988 R² (0.980 R² .. 0.996 R²)
mean 400.6 ns (389.1 ns .. 428.6 ns)
std dev 55.83 ns (28.94 ns .. 103.6 ns)
variance introduced by outliers: 94% (severely inflated)
Benchmark addNums: FINISH
Completed all 2 actions.
As noted in the comments - another variant using import Data.Foldable (foldl')
and addNums' = foldl' (+) 0
is significantly faster (thanks @User3237465!!)