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

123 lines
2.9 KiB
Rust
Raw Normal View History

// 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-28 23:54:39 -08:00
the task system. It supports a lot of old command-line arguments to
control how it runs.
*/
extern crate getopts;
2014-02-19 19:08:12 -08:00
extern crate time;
use std::os;
use std::result::{Ok, Err};
use std::task;
use std::uint;
2011-07-27 14:19:39 +02:00
fn fib(n: int) -> int {
fn pfib(tx: &Sender<int>, n: int) {
2011-07-27 14:19:39 +02:00
if n == 0 {
tx.send(0);
} else if n <= 2 {
tx.send(1);
2011-07-27 14:19:39 +02:00
} else {
let (tx1, rx) = channel();
let tx2 = tx1.clone();
task::spawn(proc() pfib(&tx2, n - 1));
let tx2 = tx1.clone();
task::spawn(proc() pfib(&tx2, n - 2));
tx.send(rx.recv() + rx.recv());
}
}
let (tx, rx) = channel();
spawn(proc() pfib(&tx, n) );
rx.recv()
}
2013-01-28 18:55:44 -08:00
struct Config {
stress: bool
}
2011-07-07 17:28:20 -07:00
2013-01-28 18:55:44 -08:00
fn parse_opts(argv: ~[~str]) -> Config {
2014-02-04 16:15:03 -08:00
let opts = ~[getopts::optflag("", "stress", "")];
2011-07-07 17:28:20 -07:00
let opt_args = argv.slice(1, argv.len());
2011-07-07 17:28:20 -07:00
2012-08-06 12:34:08 -07:00
match getopts::getopts(opt_args, opts) {
2013-01-28 18:55:44 -08:00
Ok(ref m) => {
return Config {stress: m.opt_present("stress")}
2013-01-28 18:55:44 -08:00
}
Err(_) => { fail!(); }
2011-07-07 17:28:20 -07:00
}
}
2013-05-08 17:03:17 -07:00
fn stress_task(id: int) {
let mut i = 0;
loop {
2011-07-27 14:19:39 +02:00
let n = 15;
assert_eq!(fib(n), fib(n));
2011-07-07 17:28:20 -07:00
i += 1;
error!("{}: Completed {} iterations", id, i);
2011-07-07 17:28:20 -07:00
}
}
2011-07-27 14:19:39 +02:00
fn stress(num_tasks: int) {
let mut results = ~[];
for i in range(0, num_tasks) {
2013-05-06 19:29:04 -07:00
let mut builder = task::task();
results.push(builder.future_result());
2014-01-27 18:29:50 -05:00
builder.spawn(proc() {
stress_task(i);
2014-01-27 18:29:50 -05:00
});
}
for r in results.iter() {
2013-05-06 19:29:04 -07:00
r.recv();
}
2011-07-07 17:28:20 -07: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 14:19:39 +02:00
} else {
args
};
2011-07-27 14:19:39 +02:00
2013-07-02 12:47:32 -07:00
let opts = parse_opts(args.clone());
if opts.stress {
stress(2);
} else {
let max = uint::parse_bytes(args[1].as_bytes(), 10u).unwrap() as int;
2011-07-27 14:19:39 +02:00
let num_trials = 10;
for n in range(1, max + 1) {
for _ in range(0, num_trials) {
let start = time::precise_time_ns();
let fibn = fib(n);
let stop = time::precise_time_ns();
let elapsed = stop - start;
println!("{}\t{}\t{}", n, fibn, elapsed.to_str());
}
}
}
}