-
Notifications
You must be signed in to change notification settings - Fork 0
/
treebench_sys_alloc.rs
76 lines (60 loc) · 1.87 KB
/
treebench_sys_alloc.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Tested with rustc 1.8.0-nightly (d63b8e539 2016-01-23)
#![feature(alloc_system)]
extern crate alloc_system;
use std::env;
use std::process;
use std::time::Instant;
use std::time::Duration;
enum Tree {
Leaf(i32),
Node(Box<Tree>, Box<Tree>),
}
fn build_tree(n : i32) -> Box<Tree> {
build_tree_iter(1, n)
}
fn build_tree_iter(root : i32, n : i32) -> Box<Tree> {
if n == 0 {
Box::new(Tree::Leaf(root))
} else {
Box::new(Tree::Node(build_tree_iter(root, n - 1),
build_tree_iter(root + 2^(n - 1), n - 1)))
}
}
fn add_1_tree(tree : &Tree) -> Box<Tree> {
match *tree {
Tree::Leaf(i) =>
Box::new(Tree::Leaf(i + 1)),
Tree::Node(ref left, ref right) =>
Box::new(Tree::Node(add_1_tree(&*left), add_1_tree(&*right)))
}
}
fn leftmost(tree : &Tree) -> i32 {
match *tree {
Tree::Leaf(i) => i,
Tree::Node(ref left, _) => leftmost(left)
}
}
fn main() {
let args : Vec<String> = env::args().collect();
if args.len() < 2 {
println!("Bad command line args. Expected one number (exponent).");
process::exit(1);
}
let power : i32 = args[1].parse().unwrap();
let times = 10;
let mut total : Duration = Duration::new(0, 0);
for _ in 0 .. times {
let tree = build_tree(power);
let start = Instant::now();
let tree2 = add_1_tree(&*tree);
let elapsed = start.elapsed();
println!("Test, leftmost leaf in output: {}", leftmost(&*tree2));
// println!("Took {:?}.", elapsed);
println!("Took {}.{:09}", elapsed.as_secs(), elapsed.subsec_nanos());
// Uh, += doesn't work but this works.
total = total + elapsed;
}
let average = total / times;
println!("Average of {} runs: {}.{:09} seconds.",
times, average.as_secs(), average.subsec_nanos());
}