Skip to content

Commit 4ca32e1

Browse files
committed
rewrite shootout-binarytrees.rs to match shootout directives
The old version didn't follow shootout's directives: no memory were deallocated. At the same time, parallelisation of the computation. fix #2913
1 parent 671ab42 commit 4ca32e1

File tree

1 file changed

+46
-50
lines changed

1 file changed

+46
-50
lines changed

src/test/bench/shootout-binarytrees.rs

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,81 +9,77 @@
99
// except according to those terms.
1010

1111
extern mod extra;
12+
13+
use std::iter::range_step;
1214
use extra::arena::Arena;
15+
use extra::future::Future;
1316

1417
enum Tree<'self> {
1518
Nil,
16-
Node(&'self Tree<'self>, &'self Tree<'self>, int),
19+
Node(&'self Tree<'self>, &'self Tree<'self>, int)
1720
}
1821

1922
fn item_check(t: &Tree) -> int {
2023
match *t {
21-
Nil => { return 0; }
22-
Node(left, right, item) => {
23-
return item + item_check(left) - item_check(right);
24-
}
24+
Nil => 0,
25+
Node(l, r, i) => i + item_check(l) - item_check(r)
2526
}
2627
}
2728

28-
fn bottom_up_tree<'r>(arena: &'r Arena, item: int, depth: int)
29-
-> &'r Tree<'r> {
29+
fn bottom_up_tree<'r>(arena: &'r Arena, item: int, depth: int) -> &'r Tree<'r> {
3030
if depth > 0 {
31-
return arena.alloc(
32-
|| Node(bottom_up_tree(arena, 2 * item - 1, depth - 1),
33-
bottom_up_tree(arena, 2 * item, depth - 1),
34-
item));
35-
}
36-
return arena.alloc(|| Nil);
31+
do arena.alloc {
32+
Node(bottom_up_tree(arena, 2 * item - 1, depth - 1),
33+
bottom_up_tree(arena, 2 * item, depth - 1),
34+
item)
35+
}
36+
} else {arena.alloc(|| Nil)}
3737
}
3838

3939
fn main() {
40-
use std::os;
41-
use std::int;
4240
let args = std::os::args();
43-
let args = if os::getenv("RUST_BENCH").is_some() {
44-
~[~"", ~"17"]
41+
let n = if std::os::getenv("RUST_BENCH").is_some() {
42+
17
4543
} else if args.len() <= 1u {
46-
~[~"", ~"8"]
44+
8
4745
} else {
48-
args
46+
from_str(args[1]).unwrap()
4947
};
50-
51-
let n = from_str::<int>(args[1]).unwrap();
5248
let min_depth = 4;
53-
let mut max_depth;
54-
if min_depth + 2 > n {
55-
max_depth = min_depth + 2;
56-
} else {
57-
max_depth = n;
58-
}
49+
let max_depth = if min_depth + 2 > n {min_depth + 2} else {n};
5950

60-
let stretch_arena = Arena::new();
61-
let stretch_depth = max_depth + 1;
62-
let stretch_tree = bottom_up_tree(&stretch_arena, 0, stretch_depth);
51+
{
52+
let arena = Arena::new();
53+
let depth = max_depth + 1;
54+
let tree = bottom_up_tree(&arena, 0, depth);
6355

64-
println!("stretch tree of depth {}\t check: {}",
65-
stretch_depth,
66-
item_check(stretch_tree));
56+
println!("stretch tree of depth {}\t check: {}",
57+
depth, item_check(tree));
58+
}
6759

6860
let long_lived_arena = Arena::new();
6961
let long_lived_tree = bottom_up_tree(&long_lived_arena, 0, max_depth);
70-
let mut depth = min_depth;
71-
while depth <= max_depth {
72-
let iterations = int::pow(2, (max_depth - depth + min_depth) as uint);
73-
let mut chk = 0;
74-
let mut i = 1;
75-
while i <= iterations {
76-
let mut temp_tree = bottom_up_tree(&long_lived_arena, i, depth);
77-
chk += item_check(temp_tree);
78-
temp_tree = bottom_up_tree(&long_lived_arena, -i, depth);
79-
chk += item_check(temp_tree);
80-
i += 1;
81-
}
82-
println!("{}\t trees of depth {}\t check: {}",
83-
iterations * 2, depth, chk);
84-
depth += 2;
62+
63+
let mut messages = range_step(min_depth, max_depth + 1, 2).map(|depth| {
64+
use std::int::pow;
65+
let iterations = pow(2, (max_depth - depth + min_depth) as uint);
66+
do Future::spawn {
67+
let mut chk = 0;
68+
for i in range(1, iterations + 1) {
69+
let arena = Arena::new();
70+
let a = bottom_up_tree(&arena, i, depth);
71+
let b = bottom_up_tree(&arena, -i, depth);
72+
chk += item_check(a) + item_check(b);
73+
}
74+
format!("{}\t trees of depth {}\t check: {}",
75+
iterations * 2, depth, chk)
76+
}
77+
}).to_owned_vec();
78+
79+
for message in messages.mut_iter() {
80+
println(*message.get_ref());
8581
}
82+
8683
println!("long lived tree of depth {}\t check: {}",
87-
max_depth,
88-
item_check(long_lived_tree));
84+
max_depth, item_check(long_lived_tree));
8985
}

0 commit comments

Comments
 (0)