2014-06-12 23:41:48 +02:00
|
|
|
// The Computer Language Benchmarks Game
|
|
|
|
// http://benchmarksgame.alioth.debian.org/
|
2012-12-10 17:32:48 -08:00
|
|
|
//
|
2014-06-12 23:41:48 +02:00
|
|
|
// contributed by the Rust Project Developers
|
|
|
|
|
|
|
|
// Copyright (c) 2012-2014 The Rust Project Developers
|
|
|
|
//
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions
|
|
|
|
// are met:
|
|
|
|
//
|
|
|
|
// - Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// - Redistributions in binary form must reproduce the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer in
|
|
|
|
// the documentation and/or other materials provided with the
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
// - Neither the name of "The Computer Language Benchmarks Game" nor
|
|
|
|
// the name of "The Computer Language Shootout Benchmarks" nor the
|
|
|
|
// names of its contributors may be used to endorse or promote
|
|
|
|
// products derived from this software without specific prior
|
|
|
|
// written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
// OF THE POSSIBILITY OF SUCH DAMAGE.
|
2012-12-10 17:32:48 -08:00
|
|
|
|
2015-03-05 18:33:58 -08:00
|
|
|
#![feature(rustc_private, core)]
|
|
|
|
|
2014-02-14 10:10:06 -08:00
|
|
|
extern crate arena;
|
2013-10-29 08:56:16 +01:00
|
|
|
|
|
|
|
use std::iter::range_step;
|
2015-02-17 19:00:20 -08:00
|
|
|
use std::thread;
|
2014-01-29 13:50:05 +11:00
|
|
|
use arena::TypedArena;
|
2011-03-13 18:43:39 -04:00
|
|
|
|
2015-01-10 20:14:21 +01:00
|
|
|
struct Tree<'a> {
|
|
|
|
l: Option<&'a Tree<'a>>,
|
|
|
|
r: Option<&'a Tree<'a>>,
|
|
|
|
i: i32
|
2013-02-26 14:34:00 -05:00
|
|
|
}
|
2010-06-23 21:03:09 -07:00
|
|
|
|
2015-01-10 20:14:21 +01:00
|
|
|
fn item_check(t: &Option<&Tree>) -> i32 {
|
2012-08-06 12:34:08 -07:00
|
|
|
match *t {
|
2015-01-10 20:14:21 +01:00
|
|
|
None => 0,
|
|
|
|
Some(&Tree { ref l, ref r, i }) => i + item_check(l) - item_check(r)
|
2010-06-23 21:03:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-10 20:14:21 +01:00
|
|
|
fn bottom_up_tree<'r>(arena: &'r TypedArena<Tree<'r>>, item: i32, depth: i32)
|
|
|
|
-> Option<&'r Tree<'r>> {
|
2011-07-27 14:19:39 +02:00
|
|
|
if depth > 0 {
|
2015-01-10 20:14:21 +01:00
|
|
|
let t: &Tree<'r> = arena.alloc(Tree {
|
|
|
|
l: bottom_up_tree(arena, 2 * item - 1, depth - 1),
|
|
|
|
r: bottom_up_tree(arena, 2 * item, depth - 1),
|
|
|
|
i: item
|
|
|
|
});
|
|
|
|
Some(t)
|
2014-01-06 17:03:30 -08:00
|
|
|
} else {
|
2015-01-10 20:14:21 +01:00
|
|
|
None
|
2014-01-06 17:03:30 -08:00
|
|
|
}
|
2011-03-13 18:43:39 -04:00
|
|
|
}
|
|
|
|
|
2015-01-21 11:16:00 -08:00
|
|
|
fn inner(depth: i32, iterations: i32) -> String {
|
|
|
|
let mut chk = 0;
|
|
|
|
for i in 1 .. iterations + 1 {
|
|
|
|
let arena = TypedArena::new();
|
|
|
|
let a = bottom_up_tree(&arena, i, depth);
|
|
|
|
let b = bottom_up_tree(&arena, -i, depth);
|
|
|
|
chk += item_check(&a) + item_check(&b);
|
|
|
|
}
|
|
|
|
format!("{}\t trees of depth {}\t check: {}",
|
|
|
|
iterations * 2, depth, chk)
|
|
|
|
}
|
|
|
|
|
2012-10-03 19:16:27 -07:00
|
|
|
fn main() {
|
2015-02-16 16:04:02 +02:00
|
|
|
let mut args = std::env::args();
|
2015-02-13 22:08:05 +02:00
|
|
|
let n = if std::env::var_os("RUST_BENCH").is_some() {
|
2013-10-29 08:56:16 +01:00
|
|
|
17
|
2015-02-16 16:04:02 +02:00
|
|
|
} else if args.len() <= 1 {
|
2013-10-29 08:56:16 +01:00
|
|
|
8
|
2012-01-14 19:50:23 -08:00
|
|
|
} else {
|
2015-02-16 16:04:02 +02:00
|
|
|
args.nth(1).unwrap().parse().unwrap()
|
2012-01-14 19:50:23 -08:00
|
|
|
};
|
2011-07-27 14:19:39 +02:00
|
|
|
let min_depth = 4;
|
2013-10-29 08:56:16 +01:00
|
|
|
let max_depth = if min_depth + 2 > n {min_depth + 2} else {n};
|
2012-03-28 22:23:25 -07:00
|
|
|
|
2013-10-29 08:56:16 +01:00
|
|
|
{
|
2014-01-06 17:03:30 -08:00
|
|
|
let arena = TypedArena::new();
|
2013-10-29 08:56:16 +01:00
|
|
|
let depth = max_depth + 1;
|
|
|
|
let tree = bottom_up_tree(&arena, 0, depth);
|
2012-03-28 22:23:25 -07:00
|
|
|
|
2013-10-29 08:56:16 +01:00
|
|
|
println!("stretch tree of depth {}\t check: {}",
|
2015-01-10 20:14:21 +01:00
|
|
|
depth, item_check(&tree));
|
2013-10-29 08:56:16 +01:00
|
|
|
}
|
2012-03-28 22:23:25 -07:00
|
|
|
|
2014-01-06 17:03:30 -08:00
|
|
|
let long_lived_arena = TypedArena::new();
|
2013-06-16 22:22:53 +02:00
|
|
|
let long_lived_tree = bottom_up_tree(&long_lived_arena, 0, max_depth);
|
2013-10-29 08:56:16 +01:00
|
|
|
|
2015-01-07 23:39:05 +02:00
|
|
|
let messages = range_step(min_depth, max_depth + 1, 2).map(|depth| {
|
2015-01-21 11:16:00 -08:00
|
|
|
use std::num::Int;
|
2015-02-27 21:04:15 +01:00
|
|
|
let iterations = 2.pow((max_depth - depth + min_depth) as u32);
|
2015-02-17 19:00:20 -08:00
|
|
|
thread::scoped(move || inner(depth, iterations))
|
2015-01-21 11:16:00 -08:00
|
|
|
}).collect::<Vec<_>>();
|
2013-10-29 08:56:16 +01:00
|
|
|
|
2015-01-31 20:03:04 -05:00
|
|
|
for message in messages {
|
2015-02-17 19:00:20 -08:00
|
|
|
println!("{}", message.join());
|
2011-03-13 18:43:39 -04:00
|
|
|
}
|
2013-10-29 08:56:16 +01:00
|
|
|
|
2013-09-24 22:16:43 -07:00
|
|
|
println!("long lived tree of depth {}\t check: {}",
|
2015-01-10 20:14:21 +01:00
|
|
|
max_depth, item_check(&long_lived_tree));
|
2011-08-19 15:16:48 -07:00
|
|
|
}
|