]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-pfib.rs
57a6d0e7c523d860210f5ae230400b91e5d89c7a
[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 extern crate time;
23
24 use std::os;
25 use std::result::{Ok, Err};
26 use std::task;
27 use std::uint;
28
29 fn fib(n: int) -> int {
30     fn pfib(tx: &Sender<int>, n: int) {
31         if n == 0 {
32             tx.send(0);
33         } else if n <= 2 {
34             tx.send(1);
35         } else {
36             let (tx1, rx) = channel();
37             let tx2 = tx1.clone();
38             task::spawn(proc() pfib(&tx2, n - 1));
39             let tx2 = tx1.clone();
40             task::spawn(proc() pfib(&tx2, n - 2));
41             tx.send(rx.recv() + rx.recv());
42         }
43     }
44
45     let (tx, rx) = channel();
46     spawn(proc() pfib(&tx, n) );
47     rx.recv()
48 }
49
50 struct Config {
51     stress: bool
52 }
53
54 fn parse_opts(argv: Vec<String> ) -> Config {
55     let opts = vec!(getopts::optflag("", "stress", ""));
56
57     let argv = argv.iter().map(|x| x.to_string()).collect::<Vec<_>>();
58     let opt_args = argv.slice(1, argv.len());
59
60     match getopts::getopts(opt_args, opts.as_slice()) {
61       Ok(ref m) => {
62           return Config {stress: m.opt_present("stress")}
63       }
64       Err(_) => { fail!(); }
65     }
66 }
67
68 fn stress_task(id: int) {
69     let mut i = 0;
70     loop {
71         let n = 15;
72         assert_eq!(fib(n), fib(n));
73         i += 1;
74         println!("{}: Completed {} iterations", id, i);
75     }
76 }
77
78 fn stress(num_tasks: int) {
79     let mut results = Vec::new();
80     for i in range(0, num_tasks) {
81         results.push(task::try_future(proc() {
82             stress_task(i);
83         }));
84     }
85     for r in results.move_iter() {
86         r.unwrap();
87     }
88 }
89
90 fn main() {
91     let args = os::args();
92     let args = if os::getenv("RUST_BENCH").is_some() {
93         vec!("".to_string(), "20".to_string())
94     } else if args.len() <= 1u {
95         vec!("".to_string(), "8".to_string())
96     } else {
97         args.move_iter().map(|x| x.to_string()).collect()
98     };
99
100     let opts = parse_opts(args.clone());
101
102     if opts.stress {
103         stress(2);
104     } else {
105         let max = uint::parse_bytes(args.get(1).as_bytes(), 10u).unwrap() as
106             int;
107
108         let num_trials = 10;
109
110         for n in range(1, max + 1) {
111             for _ in range(0, num_trials) {
112                 let start = time::precise_time_ns();
113                 let fibn = fib(n);
114                 let stop = time::precise_time_ns();
115
116                 let elapsed = stop - start;
117
118                 println!("{}\t{}\t{}", n, fibn, elapsed.to_str());
119             }
120         }
121     }
122 }