File: benchmark.hs

package info (click to toggle)
haskell-fgl 5.4.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 256 kB
  • ctags: 2
  • sloc: haskell: 2,050; makefile: 4
file content (138 lines) | stat: -rw-r--r-- 3,622 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
{-
  Install microbench to build this program:
  http://hackage.haskell.org/cgi-bin/hackage-scripts/package/microbench

  % ghc -O --make benchmark
  % ./benchmark
  [1 of 1] Compiling Main             ( benchmark.hs, benchmark.o )
  Linking benchmark ...
  * insNode into AVL tree: ..................
    8.877ns per iteration / 112655.53 per second.
  * insNode into PATRICIA tree: .....................
    1.788ns per iteration / 559342.86 per second.
  * insEdge into AVL tree: ...........
    2833.029ns per iteration / 352.98 per second.
  * insEdge into PATRICIA tree: ...................
    4.625ns per iteration / 216224.60 per second.
  * gmap on AVL tree: ................
    32.754ns per iteration / 30530.57 per second.
  * gmap on PATRICIA tree: .....................
    1.623ns per iteration / 616056.37 per second.
  * nmap on AVL tree: ................
    35.455ns per iteration / 28204.95 per second.
  * nmap on PATRICIA tree: .....................
    1.713ns per iteration / 583758.06 per second.
  * emap on AVL tree: ...........
    4416.303ns per iteration / 226.43 per second.
  * emap on PATRICIA tree: ...................
    4.532ns per iteration / 220663.09 per second.
-}

import Data.Graph.Inductive.Graph
import qualified Data.Graph.Inductive.Tree as AVL
import qualified Data.Graph.Inductive.PatriciaTree as Patricia
import Microbench


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

          microbench "insEdge into AVL tree" insEdgeAVL
          microbench "insEdge into PATRICIA tree" insEdgePatricia

          microbench "gmap on AVL tree" gmapAVL
          microbench "gmap on PATRICIA tree" gmapPatricia

          microbench "nmap on AVL tree" nmapAVL
          microbench "nmap on PATRICIA tree" nmapPatricia

          microbench "emap on AVL tree" emapAVL
          microbench "emap on PATRICIA tree" emapPatricia


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


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


{-# INLINE insNodes' #-}
insNodes' :: DynGraph gr => gr () b -> Int -> gr () b
insNodes' g 0 = g
insNodes' g n = let [v] = newNodes 1 g
                    g'  = insNode (v, ()) g
                in
                  insNodes' g' (n - 1)


insEdgeAVL :: Int -> AVL.UGr
insEdgeAVL n = insEdges' (insNodeAVL n) n


insEdgePatricia :: Int -> Patricia.UGr
insEdgePatricia n = insEdges' (insNodePatricia n) n


{-# INLINE insEdges' #-}
insEdges' :: DynGraph gr => gr a () -> Int -> gr a ()
insEdges' g 0 = g
insEdges' g n = let g' = insEdge (1, n, ()) g
                in
                  insEdges' g' (n - 1)


gmapAVL :: Int -> AVL.Gr Int ()
gmapAVL n
    = let g  = insNodeAVL n
          g' = gmap f g
          f (ps, v, _, ss) = (ps, v, v, ss)
      in
        g'


gmapPatricia :: Int -> Patricia.Gr Int ()
gmapPatricia n
    = let g  = insNodePatricia n
          g' = gmap f g
          f (ps, v, _, ss) = (ps, v, v, ss)
      in
        g'


nmapAVL :: Int -> AVL.Gr Int ()
nmapAVL n
    = let g   = insNodeAVL n
          g'  = nmap f g
          f _ = n
      in
        g'


nmapPatricia :: Int -> Patricia.Gr Int ()
nmapPatricia n
    = let g   = insNodePatricia n
          g'  = nmap f g
          f _ = n
      in
        g'


emapAVL :: Int -> AVL.Gr () Int
emapAVL n
    = let g   = insEdgeAVL n
          g'  = emap f g
          f _ = n
      in
        g'


emapPatricia :: Int -> Patricia.Gr () Int
emapPatricia n
    = let g   = insEdgePatricia n
          g'  = emap f g
          f _ = n
      in
        g'