-
Notifications
You must be signed in to change notification settings - Fork 0
/
treebench.pml
235 lines (210 loc) · 7.58 KB
/
treebench.pml
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
(* open Timer *)
fun putStrLn (str: string) = print (str ^ "\n")
(* fun printLargeInt (i: LargeInt.int) = putStrLn (LargeInt.toString i) *)
(* fun printLargeReal (i: LargeReal.real) = putStrLn (LargeReal.fmt (StringCvt.FIX NONE) i) *)
datatype tree =
Leaf of Int.int
| Node of tree * tree
(* type micro = LargeInt.int *)
(* type microreal = LargeReal.real *)
fun sumTree (t: tree): Int.int =
case t of
Leaf n => n
| Node (t1, t2) => (sumTree t1) + (sumTree t2)
fun add1Tree (t: tree): tree =
case t of
Leaf n => Leaf (n + 1)
| Node (t1, t2) => Node (add1Tree t1, add1Tree t2)
fun countLeaves (t: tree) : Int.int =
case t of
Leaf n => 1
| Node (t1, t2) => countLeaves t1 + countLeaves t2
(* fun buildTree (power: int): tree = *)
(* let *)
(* fun graftTree (root: Int.int, power: int): tree = *)
(* if power = 0 *)
(* then Leaf root *)
(* else Node ( graftTree (root, power-1) *)
(* , graftTree (root + Int.fromLarge (IntInf.pow (2, power-1)), power-1) *)
(* ) *)
(* in *)
(* graftTree (1, power) *)
(* end *)
fun buildTree2 (power: int): tree =
let
fun graftTree (root: Int.int, power: int): tree =
if power = 0
then Leaf root
else Node ( graftTree (root, power-1)
, graftTree (root, power-1)
)
in
graftTree (1, power)
end
fun showTreePrec (p: int, t: tree): string =
let
val openParen = if (p > 10) then "(" else ""
val closeParen = if (p > 10) then ")" else ""
in
case t of
Leaf n =>
openParen
^ "Leaf "
^ Int.toString n
^ closeParen
| Node (t1, t2) =>
openParen
^ "Node "
^ showTreePrec (11, t1)
^ " "
^ showTreePrec (11, t2)
^ closeParen
end
fun showTree (t: tree): string = showTreePrec (0, t)
(*
fun benchmarks_build (power: int, trials: int): (microreal * microreal) =
let
val _ = print "Benchmarking building"
val realTimer = startRealTimer()
fun computeTimes (its: int): tree list =
if its = 0
then []
else
let val _ = print "." in
buildTree2 power :: computeTimes (its-1)
end
val realTimer = startRealTimer()
val _ = computeTimes trials
val realTime = checkRealTimer realTimer
val microseconds = Time.toMicroseconds realTime
val _ = putStrLn ".Done!"
val _ = print "BATCHTIME: "
val _ = printLargeReal ((LargeReal.fromLargeInt microseconds) / 1000000.0)
val _ = print "\n"
val meanTime = LargeReal./ ( LargeReal.fromLargeInt microseconds
, LargeReal.fromInt trials
)
(* val sorted = FINISHME *)
val medianTime = 0.0
in
(meanTime, medianTime)
end
fun benchmarks (power: int, trials: int): (microreal * microreal) =
let
val _ = print "Benchmarking"
fun computeTimes (its: int, t: tree): tree list =
if its = 0
then []
else
let val _ = print "." in
add1Tree t :: computeTimes ((its-1), t)
end
val t = buildTree power
val realTimer = startRealTimer ()
val _ = computeTimes (trials, t)
val realTime = checkRealTimer realTimer
val microseconds = Time.toMicroseconds realTime
val _ = putStrLn ".Done!"
val _ = print "BATCHTIME: "
val _ = printLargeReal ((LargeReal.fromLargeInt microseconds) / 1000000.0)
val _ = print "\n"
val meanTime = LargeReal./ ( LargeReal.fromLargeInt microseconds
, LargeReal.fromInt trials
)
(* val sorted = FINISHME *)
val medianTime = 0.0
in
(meanTime, medianTime)
end
fun benchmarks_sum (power: int, trials: int): (microreal * microreal) =
let
val _ = print "Benchmarking"
fun computeTimes (its: int, t: tree): Int.int list =
if its = 0
then []
else
let val _ = print "." in
sumTree t :: computeTimes ((its-1), t)
end
val t = buildTree power
val realTimer = startRealTimer ()
val _ = computeTimes (trials, t)
val realTime = checkRealTimer realTimer
val microseconds = Time.toMicroseconds realTime
val _ = putStrLn ".Done!"
val _ = print "BATCHTIME: "
val _ = printLargeReal ((LargeReal.fromLargeInt microseconds) / 1000000.0)
val _ = print "\n"
val meanTime = LargeReal./ ( LargeReal.fromLargeInt microseconds
, LargeReal.fromInt trials
)
(* val sorted = FINISHME *)
val medianTime = 0.0
in
(meanTime, medianTime)
end
fun run (args : string list): (microreal * microreal) =
if EQUAL = (String.compare ((hd args), "build"))
then
let
val (power,trials) = case map Int.fromString (tl args) of
SOME i :: SOME j :: _ => (i,j)
| _ => raise Fail "Can't parse number of iterations"
val _ = print "Benchmark: build tree size 2^"
val _ = putStrLn (Int.toString power)
val _ = print " trials = "
val _ = putStrLn (Int.toString trials)
val (meanTime,median) = benchmarks_build (power, trials)
val _ = print "Mean time (seconds): "
val _ = printLargeReal (meanTime / 1000000.0)
in
(meanTime,median)
end
else if EQUAL = (String.compare ((hd args), "sum"))
then
let
val (power,trials) = case map Int.fromString (tl args) of
SOME i :: SOME j :: _ => (i,j)
| _ => raise Fail "Can't parse number of iterations"
val _ = print "Benchmark: summing all leaves of binary tree, size 2^"
val _ = putStrLn (Int.toString power)
val _ = print " trials = "
val _ = putStrLn (Int.toString trials)
val (meanTime,median) = benchmarks_sum (power, trials)
val _ = print "Mean time (seconds): "
val _ = printLargeReal (meanTime / 1000000.0)
in
(meanTime,median)
end
else
let
val (power,trials) = case map Int.fromString (tl args) of
SOME i :: SOME j :: _ => (i,j)
| _ => raise Fail "Can't parse number of iterations"
val _ = print "Benchmark: add 1 to all leaves of binary tree, size 2^"
val _ = putStrLn (Int.toString power)
val _ = print " trials = "
val _ = putStrLn (Int.toString trials)
val (meanTime,median) = benchmarks (power, trials)
val _ = print "Mean time (seconds): "
val _ = printLargeReal (meanTime / 1000000.0)
in
(meanTime,median)
end
val args = CommandLine.arguments ()
val _ = run args
*)
fun mkRun optToStr f = let
val b = Time.now ()
val ans = f ()
val e = Time.now ()
val _ = (case optToStr
of SOME tos => Print.printLn ("BATCHTIME: " ^ tos (e-b))
| NONE => ()
(* end case *))
in
ans
end
val run = mkRun (SOME Time.toString)
val runMicrosec = mkRun (SOME Time.toStringMicrosec)
val _ = run (fn () => putStrLn "Blah ... test")