1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 use std::iter::range_step;
16 use arena::TypedArena;
20 Node(&'a Tree<'a>, &'a Tree<'a>, int)
23 fn item_check(t: &Tree) -> int {
26 Node(l, r, i) => i + item_check(l) - item_check(r)
30 fn bottom_up_tree<'r>(arena: &'r TypedArena<Tree<'r>>, item: int, depth: int)
33 arena.alloc(Node(bottom_up_tree(arena, 2 * item - 1, depth - 1),
34 bottom_up_tree(arena, 2 * item, depth - 1),
42 let args = std::os::args();
43 let args = args.as_slice();
44 let n = if std::os::getenv("RUST_BENCH").is_some() {
46 } else if args.len() <= 1u {
49 from_str(args[1].as_slice()).unwrap()
52 let max_depth = if min_depth + 2 > n {min_depth + 2} else {n};
55 let arena = TypedArena::new();
56 let depth = max_depth + 1;
57 let tree = bottom_up_tree(&arena, 0, depth);
59 println!("stretch tree of depth {}\t check: {}",
60 depth, item_check(tree));
63 let long_lived_arena = TypedArena::new();
64 let long_lived_tree = bottom_up_tree(&long_lived_arena, 0, max_depth);
66 let mut messages = range_step(min_depth, max_depth + 1, 2).map(|depth| {
68 let iterations = pow(2, (max_depth - depth + min_depth) as uint);
69 Future::spawn(proc() {
71 for i in range(1, iterations + 1) {
72 let arena = TypedArena::new();
73 let a = bottom_up_tree(&arena, i, depth);
74 let b = bottom_up_tree(&arena, -i, depth);
75 chk += item_check(a) + item_check(b);
77 format_strbuf!("{}\t trees of depth {}\t check: {}",
78 iterations * 2, depth, chk)
80 }).collect::<Vec<Future<String>>>();
82 for message in messages.mut_iter() {
83 println!("{}", *message.get_ref());
86 println!("long lived tree of depth {}\t check: {}",
87 max_depth, item_check(long_lived_tree));