|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 | 11 | extern mod extra;
|
| 12 | + |
| 13 | +use std::iter::range_step; |
12 | 14 | use extra::arena::Arena;
|
| 15 | +use extra::future::Future; |
13 | 16 |
|
14 | 17 | enum Tree<'self> {
|
15 | 18 | Nil,
|
16 |
| - Node(&'self Tree<'self>, &'self Tree<'self>, int), |
| 19 | + Node(&'self Tree<'self>, &'self Tree<'self>, int) |
17 | 20 | }
|
18 | 21 |
|
19 | 22 | fn item_check(t: &Tree) -> int {
|
20 | 23 | 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) |
25 | 26 | }
|
26 | 27 | }
|
27 | 28 |
|
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> { |
30 | 30 | 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)} |
37 | 37 | }
|
38 | 38 |
|
39 | 39 | fn main() {
|
40 |
| - use std::os; |
41 |
| - use std::int; |
42 | 40 | 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 |
45 | 43 | } else if args.len() <= 1u {
|
46 |
| - ~[~"", ~"8"] |
| 44 | + 8 |
47 | 45 | } else {
|
48 |
| - args |
| 46 | + from_str(args[1]).unwrap() |
49 | 47 | };
|
50 |
| - |
51 |
| - let n = from_str::<int>(args[1]).unwrap(); |
52 | 48 | 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}; |
59 | 50 |
|
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); |
63 | 55 |
|
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 | + } |
67 | 59 |
|
68 | 60 | let long_lived_arena = Arena::new();
|
69 | 61 | 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()); |
85 | 81 | }
|
| 82 | + |
86 | 83 | 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)); |
89 | 85 | }
|
0 commit comments