Skip to content

Commit 8ebb465

Browse files
committed
Integrate benchmarks into the package
Builds off of the work by David Feuer. Closes #46
2 parents 8f8066b + cc8046c commit 8ebb465

File tree

2 files changed

+63
-8
lines changed

2 files changed

+63
-8
lines changed

fgl.cabal

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,23 @@ test-suite fgl-tests {
107107
ghc-options: -Wall
108108

109109
}
110+
111+
benchmark fgl-benchmark {
112+
default-language: Haskell98
113+
114+
type: exitcode-stdio-1.0
115+
116+
hs-source-dirs: test
117+
118+
main-is: benchmark.hs
119+
120+
other-modules: Data.Graph.Inductive.Proxy
121+
122+
build-depends: fgl
123+
, base
124+
, microbench
125+
, deepseq
126+
127+
ghc-options: -Wall
128+
129+
}

test/benchmark.hs

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
{-
2-
Install microbench to build this program:
2+
This program should generally be run using `cabal bench` or
3+
`stack bench`. To use `stack bench`, edit stack.yaml to include
4+
5+
extra-deps:
6+
- microbench-0.1
7+
8+
To run benchmarks manually, install microbench from
39
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/microbench
410
11+
then run
12+
513
% ghc -O --make benchmark
614
% ./benchmark
715
[1 of 1] Compiling Main ( benchmark.hs, benchmark.o )
@@ -28,16 +36,29 @@
2836
4.532ns per iteration / 220663.09 per second.
2937
-}
3038

31-
import Data.Graph.Inductive.Graph
32-
import qualified Data.Graph.Inductive.Tree as AVL
33-
import qualified Data.Graph.Inductive.PatriciaTree as Patricia
34-
import Microbench
39+
{-# LANGUAGE ScopedTypeVariables #-}
40+
41+
module Main (main) where
3542

43+
import Control.DeepSeq
44+
import Data.Graph.Inductive.Graph
45+
import qualified Data.Graph.Inductive.PatriciaTree as Patricia
46+
import Data.Graph.Inductive.Proxy
47+
import qualified Data.Graph.Inductive.Tree as AVL
48+
import Microbench
3649

3750
main :: IO ()
3851
main = do microbench "insNode into AVL tree" insNodeAVL
3952
microbench "insNode into PATRICIA tree" insNodePatricia
4053

54+
microbench "buildFull into AVL tree 100" (buildFullAVL 100)
55+
microbench "buildFull into AVL tree 500" (buildFullAVL 500)
56+
microbench "buildFull into AVL tree 1000" (buildFullAVL 1000)
57+
58+
microbench "buildFull into PATRICIA tree 100" (buildFullPatricia 100)
59+
microbench "buildFull into PATRICIA tree 500" (buildFullPatricia 500)
60+
microbench "buildFull into PATRICIA tree 1000" (buildFullPatricia 1000)
61+
4162
microbench "insEdge into AVL tree" insEdgeAVL
4263
microbench "insEdge into PATRICIA tree" insEdgePatricia
4364

@@ -50,14 +71,27 @@ main = do microbench "insNode into AVL tree" insNodeAVL
5071
microbench "emap on AVL tree" emapAVL
5172
microbench "emap on PATRICIA tree" emapPatricia
5273

74+
buildFullAVL :: Int -> Int -> ()
75+
buildFullAVL = buildFull (Proxy :: TreeP)
5376

5477
insNodeAVL :: Int -> AVL.UGr
5578
insNodeAVL = insNodes' empty
5679

80+
buildFullPatricia :: Int -> Int -> ()
81+
buildFullPatricia = buildFull (Proxy :: PatriciaTreeP)
5782

5883
insNodePatricia :: Int -> Patricia.UGr
5984
insNodePatricia = insNodes' empty
6085

86+
buildFull :: forall gr . (DynGraph gr, NFData (gr Int ()))
87+
=> GraphProxy gr -> Int -> Int -> ()
88+
buildFull _ sz ntimes = rnf [buildFull' i (empty :: gr Int ()) 0 sz | i <- [0..ntimes-1]]
89+
90+
buildFull' :: DynGraph gr => a -> gr a () -> Int -> Int -> gr a ()
91+
buildFull' a g n limit
92+
| n == limit = empty
93+
| otherwise = ([((), k) | k <- [0..n-1]],n,a,[((),k) | k <- [0..n-1]]) & buildFull' a g (n + 1) limit
94+
6195

6296
{-# INLINE insNodes' #-}
6397
insNodes' :: DynGraph gr => gr () b -> Int -> gr () b
@@ -79,9 +113,10 @@ insEdgePatricia n = insEdges' (insNodePatricia n) n
79113
{-# INLINE insEdges' #-}
80114
insEdges' :: DynGraph gr => gr a () -> Int -> gr a ()
81115
insEdges' g 0 = g
82-
insEdges' g n = let g' = insEdge (1, n, ()) g
116+
insEdges' g n = let n' = n - 1
117+
g' = insEdge (0, n', ()) g
83118
in
84-
insEdges' g' (n - 1)
119+
insEdges' g' n'
85120

86121

87122
gmapAVL :: Int -> AVL.Gr Int ()
@@ -135,4 +170,4 @@ emapPatricia n
135170
g' = emap f g
136171
f _ = n
137172
in
138-
g'
173+
g'

0 commit comments

Comments
 (0)