1
1
{-
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
3
9
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/microbench
4
10
11
+ then run
12
+
5
13
% ghc -O --make benchmark
6
14
% ./benchmark
7
15
[1 of 1] Compiling Main ( benchmark.hs, benchmark.o )
28
36
4.532ns per iteration / 220663.09 per second.
29
37
-}
30
38
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
35
42
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
36
49
37
50
main :: IO ()
38
51
main = do microbench " insNode into AVL tree" insNodeAVL
39
52
microbench " insNode into PATRICIA tree" insNodePatricia
40
53
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
+
41
62
microbench " insEdge into AVL tree" insEdgeAVL
42
63
microbench " insEdge into PATRICIA tree" insEdgePatricia
43
64
@@ -50,14 +71,27 @@ main = do microbench "insNode into AVL tree" insNodeAVL
50
71
microbench " emap on AVL tree" emapAVL
51
72
microbench " emap on PATRICIA tree" emapPatricia
52
73
74
+ buildFullAVL :: Int -> Int -> ()
75
+ buildFullAVL = buildFull (Proxy :: TreeP )
53
76
54
77
insNodeAVL :: Int -> AVL. UGr
55
78
insNodeAVL = insNodes' empty
56
79
80
+ buildFullPatricia :: Int -> Int -> ()
81
+ buildFullPatricia = buildFull (Proxy :: PatriciaTreeP )
57
82
58
83
insNodePatricia :: Int -> Patricia. UGr
59
84
insNodePatricia = insNodes' empty
60
85
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
+
61
95
62
96
{-# INLINE insNodes' #-}
63
97
insNodes' :: DynGraph gr => gr () b -> Int -> gr () b
@@ -79,9 +113,10 @@ insEdgePatricia n = insEdges' (insNodePatricia n) n
79
113
{-# INLINE insEdges' #-}
80
114
insEdges' :: DynGraph gr => gr a () -> Int -> gr a ()
81
115
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
83
118
in
84
- insEdges' g' (n - 1 )
119
+ insEdges' g' n'
85
120
86
121
87
122
gmapAVL :: Int -> AVL. Gr Int ()
@@ -135,4 +170,4 @@ emapPatricia n
135
170
g' = emap f g
136
171
f _ = n
137
172
in
138
- g'
173
+ g'
0 commit comments