Skip to content

Commit 467b9f3

Browse files
brsongraydon
authored andcommitted
Implement the rest of the binary trees shootout benchmark
1 parent 922f693 commit 467b9f3

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ TEST_XFAILS_RUSTC := $(addprefix test/run-pass/, \
576576
), \
577577
$(wildcard test/*fail/*.rs test/*fail/*.rc)) \
578578
test/bench/shootout/fasta.rs \
579+
test/bench/shootout/binary-trees.rs \
579580
$(wildcard test/bench/99-bottles/*rs)
580581

581582
ifdef MINGW_CROSS

src/test/bench/shootout/binary-trees.rs

+56
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
use std;
2+
3+
import std._int;
4+
15
tag tree {
26
nil;
37
node(@tree, @tree, int);
@@ -14,5 +18,57 @@ fn item_check(@tree t) -> int {
1418
}
1519
}
1620

21+
fn bottom_up_tree(int item, int depth) -> @tree{
22+
if (depth > 0) {
23+
ret @node(bottom_up_tree(2 * item - 1, depth - 1),
24+
bottom_up_tree(2 * item, depth - 1),
25+
item);
26+
} else {
27+
ret @nil;
28+
}
29+
}
30+
1731
fn main() {
32+
33+
auto n = 8;
34+
auto min_depth = 4;
35+
auto max_depth;
36+
if (min_depth + 2 > n) {
37+
max_depth = min_depth + 2;
38+
} else {
39+
max_depth = n;
40+
}
41+
42+
auto stretch_depth = max_depth + 1;
43+
44+
auto stretch_tree = bottom_up_tree(0, stretch_depth);
45+
log #fmt("stretch tree of depth %d\t check: %d",
46+
stretch_depth, item_check(stretch_tree));
47+
48+
auto long_lived_tree = bottom_up_tree(0, max_depth);
49+
50+
auto depth = min_depth;
51+
while (depth <= max_depth) {
52+
auto iterations = _int.pow(2, (max_depth - depth + min_depth) as uint);
53+
auto chk = 0;
54+
55+
auto i = 1;
56+
while (i <= iterations) {
57+
auto temp_tree = bottom_up_tree(i, depth);
58+
chk += item_check(temp_tree);
59+
60+
temp_tree = bottom_up_tree(-i, depth);
61+
chk += item_check(temp_tree);
62+
63+
i += 1;
64+
}
65+
66+
log #fmt("%d\t trees of depth %d\t check: %d",
67+
iterations * 2, depth, chk);
68+
69+
depth += 2;
70+
}
71+
72+
log #fmt("long lived trees of depth %d\t check: %d",
73+
max_depth, item_check(long_lived_tree));
1874
}

0 commit comments

Comments
 (0)