Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 119 lines (95 sloc) 2.656 kb
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
1 // -*- rust -*-
050170d @marijnh Remove remaining uses of iter and for-each
marijnh authored
2 // xfail-pretty
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
3
4 /*
5 A parallel version of fibonacci numbers.
6
7 This version is meant mostly as a way of stressing and benchmarking
8 the task system. It supports a lot of command-line arguments to
9 control how it runs.
10
11 */
12
e653d49 @pcwalton rustc: Remove legacy mode inference, unless #[legacy_modes] is used
pcwalton authored
13 #[legacy_modes];
14
ea01ee2 @brson Convert 'use' to 'extern mod'. Remove old 'use' syntax
brson authored
15 extern mod std;
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
16
f686896 @pcwalton test: "import" -> "use"
pcwalton authored
17 use std::{time, getopts};
18 use io::WriterUtil;
19 use int::range;
20 use pipes::Port;
21 use pipes::Chan;
22 use pipes::send;
23 use pipes::recv;
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
24
f686896 @pcwalton test: "import" -> "use"
pcwalton authored
25 use core::result;
26 use result::{Ok, Err};
fa27724 @boggle std: getopts now uses result::t (fixes #1289)
boggle authored
27
df7f21d @marijnh Reformat for new syntax
marijnh authored
28 fn fib(n: int) -> int {
cfbc7cb @brson Convert core::pipes to camel case
brson authored
29 fn pfib(c: Chan<int>, n: int) {
df7f21d @marijnh Reformat for new syntax
marijnh authored
30 if n == 0 {
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
31 c.send(0);
518dc52 @brson Reformat
brson authored
32 } else if n <= 2 {
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
33 c.send(1);
df7f21d @marijnh Reformat for new syntax
marijnh authored
34 } else {
5f57588 @eholk Update the rock-paper-scissors example in the tutorial, and rename so…
eholk authored
35 let p = pipes::PortSet();
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
36 let ch = p.chan();
d1fc2b5 @brson Convert to new closure syntax
brson authored
37 task::spawn(|| pfib(ch, n - 1) );
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
38 let ch = p.chan();
d1fc2b5 @brson Convert to new closure syntax
brson authored
39 task::spawn(|| pfib(ch, n - 2) );
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
40 c.send(p.recv() + p.recv());
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
41 }
42 }
43
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
44 let (ch, p) = pipes::stream();
d1fc2b5 @brson Convert to new closure syntax
brson authored
45 let t = task::spawn(|| pfib(ch, n) );
f76a462 @eholk Convert pfib to pipes. This is a useful stress test.
eholk authored
46 p.recv()
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
47 }
48
df7f21d @marijnh Reformat for new syntax
marijnh authored
49 type config = {stress: bool};
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
50
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
51 fn parse_opts(argv: ~[~str]) -> config {
52 let opts = ~[getopts::optflag(~"stress")];
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
53
f05a91a @brson Rename std::ivec to std::vec
brson authored
54 let opt_args = vec::slice(argv, 1u, vec::len(argv));
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
55
ecaf9e3 @brson Convert alt to match. Stop parsing alt
brson authored
56 match getopts::getopts(opt_args, opts) {
0c6e470 @brson Convert core::result to camel case
brson authored
57 Ok(m) => { return {stress: getopts::opt_present(m, ~"stress")} }
58 Err(_) => { fail; }
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
59 }
60 }
61
ebc61e3 @brson Remove temporary fn# syntax
brson authored
62 fn stress_task(&&id: int) {
dc07280 @nikomatsakis make --enforce-mut-vars always on, add mut annotations to remaining f…
nikomatsakis authored
63 let mut i = 0;
321fd80 @catamorphism Add an infinite loop construct
catamorphism authored
64 loop {
df7f21d @marijnh Reformat for new syntax
marijnh authored
65 let n = 15;
66 assert (fib(n) == fib(n));
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
67 i += 1;
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
68 error!("%d: Completed %d iterations", id, i);
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
69 }
70 }
71
df7f21d @marijnh Reformat for new syntax
marijnh authored
72 fn stress(num_tasks: int) {
98e161f @msullivan Switch the compiler over to using ~[] notation instead of []/~. Close…
msullivan authored
73 let mut results = ~[];
d1fc2b5 @brson Convert to new closure syntax
brson authored
74 for range(0, num_tasks) |i| {
18ac4a8 @bblum Change task().future_result's argument mode
bblum authored
75 do task::task().future_result(|+r| {
8e86cd0 @olsonjeffery test: bunch of test cleanup from sendable_futures change
olsonjeffery authored
76 vec::push(results, r);
b30a58a @bblum Convert bench and run-pass tests to new task_builder interface
bblum authored
77 }).spawn {
78 stress_task(i);
79 }
3b5b932 @marijnh Remove some semicolons after block calls
marijnh authored
80 }
9600744 @graydon fix more fallout from future de-mode-ing.
graydon authored
81 for results.each |r| { future::get(&r); }
2d57b25 Added a stress test mode to pfib.
Eric Holk authored
82 }
83
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
84 fn main(args: ~[~str]) {
85 let args = if os::getenv(~"RUST_BENCH").is_some() {
86 ~[~"", ~"20"]
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
87 } else if args.len() <= 1u {
92743dc @msullivan Move the world over to using the new style string literals and types.…
msullivan authored
88 ~[~"", ~"8"]
df7f21d @marijnh Reformat for new syntax
marijnh authored
89 } else {
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
90 args
91 };
df7f21d @marijnh Reformat for new syntax
marijnh authored
92
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
93 let opts = parse_opts(args);
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
94
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
95 if opts.stress {
96 stress(2);
97 } else {
b73f801 @erickt int/uint parse_buf => parse_bytes (#3444)
erickt authored
98 let max = option::get(uint::parse_bytes(str::to_bytes(args[1]),
99 10u)) as int;
df7f21d @marijnh Reformat for new syntax
marijnh authored
100
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
101 let num_trials = 10;
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
102
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
103 let out = io::stdout();
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
104
d1fc2b5 @brson Convert to new closure syntax
brson authored
105 for range(1, max + 1) |n| {
106 for range(0, num_trials) |i| {
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
107 let start = time::precise_time_ns();
108 let fibn = fib(n);
109 let stop = time::precise_time_ns();
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
110
4fec4cd @brson bench: Add hard mode to benchmarks. Activate with RUST_BENCH.
brson authored
111 let elapsed = stop - start;
39151f2 @graydon Prohibit trailing whitespace under 'tidy' script. Clean up all caught…
graydon authored
112
29f32b4 @paulstansifer `m1!{...}` -> `m1!(...)`
paulstansifer authored
113 out.write_line(fmt!("%d\t%d\t%s", n, fibn,
114 u64::str(elapsed)));
3b5b932 @marijnh Remove some semicolons after block calls
marijnh authored
115 }
e192755 Added a new version of pfib that's better suited for benchmarking the…
Eric Holk authored
116 }
117 }
f91351a @lkuper The wonky for...in... whitespace was bothering me. Sorry!
lkuper authored
118 }
Something went wrong with that request. Please try again.