Skip to content

Commit 9b46e87

Browse files
tedhorstbrson
authored andcommitted
add third arg for output path, default to no output
1 parent 63ae16f commit 9b46e87

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

src/test/bench/shootout-mandelbrot.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
// http://shootout.alioth.debian.org/
33
// u64q/program.php?test=mandelbrot&lang=python3&id=2
44
//
5-
// takes 2 optional numeric args: square image size and yield frequency
5+
// takes 3 optional args:
6+
// square image size, defaults to 80_u
7+
// yield frequency, defaults to 10_u (yield every 10 spawns)
8+
// output path, default is "" (no output), "-" means stdout
9+
//
610
// in the shootout, they use 16000 as image size
711
// yield frequency doesn't seem to have much effect
812
//
9-
// writes pbm image to stdout
13+
// writes pbm image to output path
1014

1115
use std;
1216
import std::io::writer_util;
@@ -72,12 +76,33 @@ fn chanmb(i: uint, size: uint, ch: comm::chan<line>) -> ()
7276
comm::send(ch, {i:i, b:crv});
7377
}
7478

75-
fn writer(writech: comm::chan<comm::chan<line>>, size: uint)
79+
type devnull = {dn: int};
80+
81+
impl of std::io::writer for devnull {
82+
fn write(_b: [const u8]) {}
83+
fn seek(_i: int, _s: std::io::seek_style) {}
84+
fn tell() -> uint {0_u}
85+
fn flush() -> int {0}
86+
}
87+
88+
fn writer(path: str, writech: comm::chan<comm::chan<line>>, size: uint)
7689
{
7790
let p: comm::port<line> = comm::port();
7891
let ch = comm::chan(p);
7992
comm::send(writech, ch);
80-
let cout = std::io::stdout();
93+
let cout: std::io::writer = alt path {
94+
"" {
95+
{dn: 0} as std::io::writer
96+
}
97+
"-" {
98+
std::io::stdout()
99+
}
100+
_ {
101+
result::get(
102+
std::io::file_writer(path,
103+
[std::io::create, std::io::truncate]))
104+
}
105+
};
81106
cout.write_line("P4");
82107
cout.write_line(#fmt("%u %u", size, size));
83108
let lines = std::map::new_uint_hash();
@@ -125,10 +150,16 @@ fn main(argv: [str])
125150
else {
126151
uint::from_str(argv[2])
127152
};
153+
let path = if vec::len(argv) < 4_u {
154+
""
155+
}
156+
else {
157+
argv[3]
158+
};
128159
let writep = comm::port();
129160
let writech = comm::chan(writep);
130161
task::spawn {
131-
|| writer(writech, size);
162+
|| writer(path, writech, size);
132163
};
133164
let ch = comm::recv(writep);
134165
uint::range(0_u, size) {

0 commit comments

Comments
 (0)