]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-pfib.rs
doc: remove incomplete sentence
[rust.git] / src / test / bench / shootout-pfib.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11
12 /*
13   A parallel version of fibonacci numbers.
14
15   This version is meant mostly as a way of stressing and benchmarking
16   the task system. It supports a lot of old command-line arguments to
17   control how it runs.
18
19 */
20
21 extern crate getopts;
22
23 use std::sync::mpsc::{channel, Sender};
24 use std::os;
25 use std::result::Result::{Ok, Err};
26 use std::str::from_str;
27 use std::thread::Thread;
28 use std::time::Duration;
29
30 fn fib(n: int) -> int {
31     fn pfib(tx: &Sender<int>, n: int) {
32         if n == 0 {
33             tx.send(0).unwrap();
34         } else if n <= 2 {
35             tx.send(1).unwrap();
36         } else {
37             let (tx1, rx) = channel();
38             let tx2 = tx1.clone();
39             Thread::spawn(move|| pfib(&tx2, n - 1)).detach();
40             let tx2 = tx1.clone();
41             Thread::spawn(move|| pfib(&tx2, n - 2)).detach();
42             tx.send(rx.recv().unwrap() + rx.recv().unwrap());
43         }
44     }
45
46     let (tx, rx) = channel();
47     Thread::spawn(move|| pfib(&tx, n) ).detach();
48     rx.recv().unwrap()
49 }
50
51 struct Config {
52     stress: bool
53 }
54
55 fn parse_opts(argv: Vec<String> ) -> Config {
56     let opts = vec!(getopts::optflag("", "stress", ""));
57
58     let argv = argv.iter().map(|x| x.to_string()).collect::<Vec<_>>();
59     let opt_args = argv.slice(1, argv.len());
60
61     match getopts::getopts(opt_args, opts.as_slice()) {
62       Ok(ref m) => {
63           return Config {stress: m.opt_present("stress")}
64       }
65       Err(_) => { panic!(); }
66     }
67 }
68
69 fn stress_task(id: int) {
70     let mut i = 0i;
71     loop {
72         let n = 15i;
73         assert_eq!(fib(n), fib(n));
74         i += 1;
75         println!("{}: Completed {} iterations", id, i);
76     }
77 }
78
79 fn stress(num_tasks: int) {
80     let mut results = Vec::new();
81     for i in range(0, num_tasks) {
82         results.push(Thread::spawn(move|| {
83             stress_task(i);
84         }));
85     }
86     for r in results.into_iter() {
87         let _ = r.join();
88     }
89 }
90
91 fn main() {
92     let args = os::args();
93     let args = if os::getenv("RUST_BENCH").is_some() {
94         vec!("".to_string(), "20".to_string())
95     } else if args.len() <= 1u {
96         vec!("".to_string(), "8".to_string())
97     } else {
98         args.into_iter().map(|x| x.to_string()).collect()
99     };
100
101     let opts = parse_opts(args.clone());
102
103     if opts.stress {
104         stress(2);
105     } else {
106         let max = from_str::<uint>(args[1].as_slice()).unwrap() as int;
107
108         let num_trials = 10;
109
110         for n in range(1, max + 1) {
111             for _ in range(0u, num_trials) {
112                 let mut fibn = None;
113                 let dur = Duration::span(|| fibn = Some(fib(n)));
114                 let fibn = fibn.unwrap();
115
116                 println!("{}\t{}\t{}", n, fibn, dur);
117             }
118         }
119     }
120 }