Skip to content

Commit 8774493

Browse files
committed
test: Rewrite binarytrees to use arenas
Perf isn't bad now. Still 50% slower than Java, but faster than other GC'd languages.
1 parent 166d14e commit 8774493

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

src/test/bench/shootout-binarytrees.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std;
2+
import std::arena;
3+
import std::arena::arena;
24

3-
enum tree { nil, node(~tree, ~tree, int), }
5+
enum tree { nil, node(&tree, &tree, int), }
46

5-
fn item_check(t: ~tree) -> int {
7+
fn item_check(t: &tree) -> int {
68
alt *t {
79
nil { ret 0; }
810
node(left, right, item) {
@@ -11,11 +13,13 @@ fn item_check(t: ~tree) -> int {
1113
}
1214
}
1315

14-
fn bottom_up_tree(item: int, depth: int) -> ~tree {
16+
fn bottom_up_tree(arena: &a.arena::arena, item: int, depth: int) -> &a.tree {
1517
if depth > 0 {
16-
ret ~node(bottom_up_tree(2 * item - 1, depth - 1),
17-
bottom_up_tree(2 * item, depth - 1), item);
18-
} else { ret ~nil; }
18+
ret new(*arena) node(bottom_up_tree(arena, 2 * item - 1, depth - 1),
19+
bottom_up_tree(arena, 2 * item, depth - 1),
20+
item);
21+
}
22+
ret new(*arena) nil;
1923
}
2024

2125
fn main(args: [str]) {
@@ -28,22 +32,29 @@ fn main(args: [str]) {
2832
let mut max_depth;
2933
if min_depth + 2 > n {
3034
max_depth = min_depth + 2;
31-
} else { max_depth = n; }
35+
} else {
36+
max_depth = n;
37+
}
38+
39+
let stretch_arena = arena::arena();
3240
let stretch_depth = max_depth + 1;
33-
let stretch_tree = bottom_up_tree(0, stretch_depth);
41+
let stretch_tree = bottom_up_tree(&stretch_arena, 0, stretch_depth);
42+
3443
io::println(#fmt("stretch tree of depth %d\t check: %d",
3544
stretch_depth,
3645
item_check(stretch_tree)));
37-
let long_lived_tree = bottom_up_tree(0, max_depth);
46+
47+
let long_lived_arena = arena::arena();
48+
let long_lived_tree = bottom_up_tree(&long_lived_arena, 0, max_depth);
3849
let mut depth = min_depth;
3950
while depth <= max_depth {
4051
let iterations = int::pow(2, (max_depth - depth + min_depth) as uint);
4152
let mut chk = 0;
4253
let mut i = 1;
4354
while i <= iterations {
44-
let mut temp_tree = bottom_up_tree(i, depth);
55+
let mut temp_tree = bottom_up_tree(&long_lived_arena, i, depth);
4556
chk += item_check(temp_tree);
46-
temp_tree = bottom_up_tree(-i, depth);
57+
temp_tree = bottom_up_tree(&long_lived_arena, -i, depth);
4758
chk += item_check(temp_tree);
4859
i += 1;
4960
}

0 commit comments

Comments
 (0)