-
Notifications
You must be signed in to change notification settings - Fork 0
/
treebench.ml
83 lines (74 loc) · 2.26 KB
/
treebench.ml
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
type tree =
| Leaf of int
| Node of tree * tree
let build_tree (n : int) : tree =
let rec go root n =
if n == 0 then
Leaf root
else
Node (go root (n - 1), go (root + (2 * (n - 1))) (n - 1))
in
go 1 n
let build_tree2 (n : int) : tree =
let rec go root n =
if n == 0 then
Leaf root
else
Node (go root (n - 1), go root (n - 1))
in
go 1 n
let rec sum_tree = function
| Leaf i -> i
| Node (x, y) -> (sum_tree x) + (sum_tree y)
let rec add1_tree = function
| Leaf i -> Leaf (i + 1)
| Node (x, y) -> Node (add1_tree x, add1_tree y)
let rec leftmost = function
| Leaf i -> i
| Node (x, _) -> leftmost x
let () = (
if Array.length Sys.argv <> 4 then
raise (Failure "Bad command line args. Expected one string and two numbers (exponent).")
else (
let mode = Sys.argv.(1) in
let power = int_of_string (Sys.argv.(2)) in
let iters = int_of_string (Sys.argv.(3)) in
if mode = "build" then
(
Printf.printf "Benchmark build\n";
let start = Sys.time () in
for _i = 0 to iters do
let tree = build_tree2 power in
Printf.printf "(Test, leftmost leaf in output: %d)\n" (leftmost tree);
done;
let end_ = Sys.time() in
Printf.printf "BATCHTIME: %f\n" (end_ -. start);
)
else if mode = "sum" then
(
Printf.printf "Benchmark sum\n";
let start = Sys.time () in
let tree = build_tree power in
for _i = 0 to iters do
let ret = sum_tree tree in
Printf.printf "Sum: %i\n" ret;
done;
let end_ = Sys.time() in
Printf.printf "Took %f seconds.\n" (end_ -. start);
Printf.printf "BATCHTIME: %f\n" (end_ -. start);
)
else
(
Printf.printf "Benchmark add1\n";
let start = Sys.time () in
let tree = build_tree power in
for _i = 0 to iters do
let ret = add1_tree tree in
Printf.printf "(Test, leftmost leaf in output: %d)\n" (leftmost ret);
done;
let end_ = Sys.time() in
Printf.printf "Took %f seconds.\n" (end_ -. start);
Printf.printf "BATCHTIME: %f\n" (end_ -. start);
)
)
)