]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-binarytrees.rs
Doc says to avoid mixing allocator instead of forbiding it
[rust.git] / src / test / bench / shootout-binarytrees.rs
1 // The Computer Language Benchmarks Game
2 // http://benchmarksgame.alioth.debian.org/
3 //
4 // contributed by the Rust Project Developers
5
6 // Copyright (c) 2012-2014 The Rust Project Developers
7 //
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions
12 // are met:
13 //
14 // - Redistributions of source code must retain the above copyright
15 //   notice, this list of conditions and the following disclaimer.
16 //
17 // - Redistributions in binary form must reproduce the above copyright
18 //   notice, this list of conditions and the following disclaimer in
19 //   the documentation and/or other materials provided with the
20 //   distribution.
21 //
22 // - Neither the name of "The Computer Language Benchmarks Game" nor
23 //   the name of "The Computer Language Shootout Benchmarks" nor the
24 //   names of its contributors may be used to endorse or promote
25 //   products derived from this software without specific prior
26 //   written permission.
27 //
28 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 // OF THE POSSIBILITY OF SUCH DAMAGE.
40
41 extern crate arena;
42
43 use std::iter::range_step;
44 use std::sync::Future;
45 use arena::TypedArena;
46
47 enum Tree<'a> {
48     Nil,
49     Node(&'a Tree<'a>, &'a Tree<'a>, int)
50 }
51
52 fn item_check(t: &Tree) -> int {
53     match *t {
54         Nil => 0,
55         Node(l, r, i) => i + item_check(l) - item_check(r)
56     }
57 }
58
59 fn bottom_up_tree<'r>(arena: &'r TypedArena<Tree<'r>>, item: int, depth: int)
60                   -> &'r Tree<'r> {
61     if depth > 0 {
62         arena.alloc(Node(bottom_up_tree(arena, 2 * item - 1, depth - 1),
63                          bottom_up_tree(arena, 2 * item, depth - 1),
64                          item))
65     } else {
66         arena.alloc(Nil)
67     }
68 }
69
70 fn main() {
71     let args = std::os::args();
72     let args = args.as_slice();
73     let n = if std::os::getenv("RUST_BENCH").is_some() {
74         17
75     } else if args.len() <= 1u {
76         8
77     } else {
78         from_str(args[1].as_slice()).unwrap()
79     };
80     let min_depth = 4;
81     let max_depth = if min_depth + 2 > n {min_depth + 2} else {n};
82
83     {
84         let arena = TypedArena::new();
85         let depth = max_depth + 1;
86         let tree = bottom_up_tree(&arena, 0, depth);
87
88         println!("stretch tree of depth {}\t check: {}",
89                  depth, item_check(tree));
90     }
91
92     let long_lived_arena = TypedArena::new();
93     let long_lived_tree = bottom_up_tree(&long_lived_arena, 0, max_depth);
94
95     let mut messages = range_step(min_depth, max_depth + 1, 2).map(|depth| {
96             use std::num::pow;
97             let iterations = pow(2i, (max_depth - depth + min_depth) as uint);
98             Future::spawn(proc() {
99                 let mut chk = 0;
100                 for i in range(1, iterations + 1) {
101                     let arena = TypedArena::new();
102                     let a = bottom_up_tree(&arena, i, depth);
103                     let b = bottom_up_tree(&arena, -i, depth);
104                     chk += item_check(a) + item_check(b);
105                 }
106                 format!("{}\t trees of depth {}\t check: {}",
107                         iterations * 2, depth, chk)
108             })
109         }).collect::<Vec<Future<String>>>();
110
111     for message in messages.mut_iter() {
112         println!("{}", *message.get_ref());
113     }
114
115     println!("long lived tree of depth {}\t check: {}",
116              max_depth, item_check(long_lived_tree));
117 }