]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-pfib.rs
test: Remove all uses of `~str` from the test suite.
[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::task::TaskBuilder;
28 use std::uint;
29
30 fn fib(n: int) -> int {
31     fn pfib(tx: &Sender<int>, n: int) {
32         if n == 0 {
33             tx.send(0);
34         } else if n <= 2 {
35             tx.send(1);
36         } else {
37             let (tx1, rx) = channel();
38             let tx2 = tx1.clone();
39             task::spawn(proc() pfib(&tx2, n - 1));
40             let tx2 = tx1.clone();
41             task::spawn(proc() pfib(&tx2, n - 2));
42             tx.send(rx.recv() + rx.recv());
43         }
44     }
45
46     let (tx, rx) = channel();
47     spawn(proc() pfib(&tx, n) );
48     rx.recv()
49 }
50
51 struct Config {
52     stress: bool
53 }
54
55 fn parse_opts(argv: Vec<StrBuf> ) -> Config {
56     let opts = vec!(getopts::optflag("", "stress", ""));
57
58     let argv = argv.iter().map(|x| x.to_str()).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(_) => { fail!(); }
66     }
67 }
68
69 fn stress_task(id: int) {
70     let mut i = 0;
71     loop {
72         let n = 15;
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         let mut builder = TaskBuilder::new();
83         results.push(builder.future_result());
84         builder.spawn(proc() {
85             stress_task(i);
86         });
87     }
88     for r in results.iter() {
89         r.recv();
90     }
91 }
92
93 fn main() {
94     let args = os::args();
95     let args = if os::getenv("RUST_BENCH").is_some() {
96         vec!("".to_strbuf(), "20".to_strbuf())
97     } else if args.len() <= 1u {
98         vec!("".to_strbuf(), "8".to_strbuf())
99     } else {
100         args.move_iter().map(|x| x.to_strbuf()).collect()
101     };
102
103     let opts = parse_opts(args.clone());
104
105     if opts.stress {
106         stress(2);
107     } else {
108         let max = uint::parse_bytes(args.get(1).as_bytes(), 10u).unwrap() as
109             int;
110
111         let num_trials = 10;
112
113         for n in range(1, max + 1) {
114             for _ in range(0, num_trials) {
115                 let start = time::precise_time_ns();
116                 let fibn = fib(n);
117                 let stop = time::precise_time_ns();
118
119                 let elapsed = stop - start;
120
121                 println!("{}\t{}\t{}", n, fibn, elapsed.to_str());
122             }
123         }
124     }
125 }