rust/src/test/bench/shootout-pfib.rs

130 lines
3.0 KiB
Rust
Raw Normal View History

// -*- rust -*-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*
A parallel version of fibonacci numbers.
This version is meant mostly as a way of stressing and benchmarking
2013-01-29 01:54:39 -06:00
the task system. It supports a lot of old command-line arguments to
control how it runs.
*/
#[legacy_modes];
extern mod std;
2012-09-05 14:32:05 -05:00
use std::{time, getopts};
2013-02-02 05:10:12 -06:00
use core::int::range;
use core::comm::*;
use core::io::WriterUtil;
2012-09-05 14:32:05 -05:00
use core::result;
use core::result::{Ok, Err};
2011-07-27 07:19:39 -05:00
fn fib(n: int) -> int {
2012-08-28 13:11:15 -05:00
fn pfib(c: Chan<int>, n: int) {
2011-07-27 07:19:39 -05:00
if n == 0 {
c.send(0);
} else if n <= 2 {
c.send(1);
2011-07-27 07:19:39 -05:00
} else {
let p = PortSet::new();
let ch = p.chan();
2013-02-15 04:44:18 -06:00
task::spawn(|| pfib(ch, n - 1) );
let ch = p.chan();
2013-02-15 04:44:18 -06:00
task::spawn(|| pfib(ch, n - 2) );
c.send(p.recv() + p.recv());
}
}
2013-02-02 05:10:12 -06:00
let (p, ch) = stream();
2013-02-15 04:44:18 -06:00
let _t = task::spawn(|| pfib(ch, n) );
p.recv()
}
2013-01-28 20:55:44 -06:00
struct Config {
stress: bool
}
2011-07-07 19:28:20 -05:00
2013-01-28 20:55:44 -06:00
fn parse_opts(argv: ~[~str]) -> Config {
let opts = ~[getopts::optflag(~"stress")];
2011-07-07 19:28:20 -05:00
2013-01-28 20:55:44 -06:00
let opt_args = vec::slice(argv, 1, argv.len());
2011-07-07 19:28:20 -05:00
2012-08-06 14:34:08 -05:00
match getopts::getopts(opt_args, opts) {
2013-01-28 20:55:44 -06:00
Ok(ref m) => {
return Config {stress: getopts::opt_present(m, ~"stress")}
}
Err(_) => { fail!(); }
2011-07-07 19:28:20 -05:00
}
}
2011-10-20 22:34:04 -05:00
fn stress_task(&&id: int) {
let mut i = 0;
loop {
2011-07-27 07:19:39 -05:00
let n = 15;
2013-03-28 20:39:09 -05:00
assert!((fib(n) == fib(n)));
2011-07-07 19:28:20 -05:00
i += 1;
2012-08-22 19:24:52 -05:00
error!("%d: Completed %d iterations", id, i);
2011-07-07 19:28:20 -05:00
}
}
2011-07-27 07:19:39 -05:00
fn stress(num_tasks: int) {
let mut results = ~[];
2012-06-30 18:19:07 -05:00
for range(0, num_tasks) |i| {
do task::task().future_result(|+r| {
2013-02-15 04:44:18 -06:00
results.push(r);
}).spawn {
stress_task(i);
}
}
for results.each |r| { r.recv(); }
2011-07-07 19:28:20 -05:00
}
fn main() {
let args = os::args();
let args = if os::getenv(~"RUST_BENCH").is_some() {
~[~"", ~"20"]
} else if args.len() <= 1u {
~[~"", ~"8"]
2011-07-27 07:19:39 -05:00
} else {
args
};
2011-07-27 07:19:39 -05:00
let opts = parse_opts(copy args);
if opts.stress {
stress(2);
} else {
2012-09-21 21:37:57 -05:00
let max = uint::parse_bytes(str::to_bytes(args[1]),
10u).get() as int;
2011-07-27 07:19:39 -05:00
let num_trials = 10;
let out = io::stdout();
2012-06-30 18:19:07 -05:00
for range(1, max + 1) |n| {
2012-09-19 00:44:34 -05:00
for range(0, num_trials) |_i| {
let start = time::precise_time_ns();
let fibn = fib(n);
let stop = time::precise_time_ns();
let elapsed = stop - start;
2012-08-22 19:24:52 -05:00
out.write_line(fmt!("%d\t%d\t%s", n, fibn,
u64::to_str(elapsed)));
}
}
}
}