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
39
+ {-# LANGUAGE CPP #-}
40
+ {-# LANGUAGE ScopedTypeVariables #-}
41
+ #if __GLASGOW_HASKELL__ >= 708
42
+
31
43
import Data.Graph.Inductive.Graph
32
44
import qualified Data.Graph.Inductive.Tree as AVL
33
45
import qualified Data.Graph.Inductive.PatriciaTree as Patricia
34
46
import Microbench
35
-
47
+ import Data.Foldable (foldl' )
48
+ import Control.DeepSeq
49
+ import Data.Proxy
36
50
37
51
main :: IO ()
38
52
main = do microbench " insNode into AVL tree" insNodeAVL
39
53
microbench " insNode into PATRICIA tree" insNodePatricia
40
54
41
- microbench " insEdge into AVL tree" insEdgeAVL
55
+ microbench " buildFull into PATRICIA tree 100" (buildFullPatricia 100 )
56
+ microbench " buildFull into PATRICIA tree 500" (buildFullPatricia 500 )
57
+ microbench " buildFull into PATRICIA tree 1000" (buildFullPatricia 1000 )
58
+
59
+ -- microbench "insEdge into AVL tree" insEdgeAVL
42
60
microbench " insEdge into PATRICIA tree" insEdgePatricia
43
61
44
62
microbench " gmap on AVL tree" gmapAVL
@@ -47,17 +65,27 @@ main = do microbench "insNode into AVL tree" insNodeAVL
47
65
microbench " nmap on AVL tree" nmapAVL
48
66
microbench " nmap on PATRICIA tree" nmapPatricia
49
67
50
- microbench " emap on AVL tree" emapAVL
68
+ -- microbench "emap on AVL tree" emapAVL
51
69
microbench " emap on PATRICIA tree" emapPatricia
52
70
53
71
54
72
insNodeAVL :: Int -> AVL. UGr
55
73
insNodeAVL = insNodes' empty
56
74
75
+ buildFullPatricia :: Int -> Int -> ()
76
+ buildFullPatricia sz i = buildFull (Proxy :: Proxy Patricia. Gr ) sz i
57
77
58
78
insNodePatricia :: Int -> Patricia. UGr
59
79
insNodePatricia = insNodes' empty
60
80
81
+ buildFull :: forall gr proxy . (DynGraph gr , NFData (gr Int () )) => proxy gr -> Int -> Int -> ()
82
+ buildFull _ sz ntimes = rnf [buildFull' i (empty :: gr Int () ) 0 sz | i <- [0 .. ntimes- 1 ]]
83
+
84
+ buildFull' :: DynGraph gr => a -> gr a () -> Int -> Int -> gr a ()
85
+ buildFull' a g n limit
86
+ | n == limit = empty
87
+ | otherwise = ([(() , k) | k <- [0 .. n- 1 ]],n,a,[(() ,k) | k <- [0 .. n- 1 ]]) & buildFull' a g (n + 1 ) limit
88
+
61
89
62
90
{-# INLINE insNodes' #-}
63
91
insNodes' :: DynGraph gr => gr () b -> Int -> gr () b
@@ -135,4 +163,9 @@ emapPatricia n
135
163
g' = emap f g
136
164
f _ = n
137
165
in
138
- g'
166
+ g'
167
+
168
+ #else
169
+ main :: IO ()
170
+ main = putStrLn " Benchmarks are only supported for GHC 7.8 and above."
171
+ #endif
0 commit comments