12
12
// http://shootout.alioth.debian.org/
13
13
// u64q/program.php?test=mandelbrot&lang=python3&id=2
14
14
//
15
- // takes 2 optional args:
15
+ // takes 3 optional args:
16
16
// square image size, defaults to 80_u
17
+ // yield frequency, defaults to 10_u (yield every 10 spawns)
17
18
// output path, default is "" (no output), "-" means stdout
18
19
//
19
20
// in the shootout, they use 16000 as image size
21
+ // yield frequency doesn't seem to have much effect
20
22
//
21
23
// writes pbm image to output path
22
24
25
+ #[ legacy_modes] ;
26
+
23
27
extern mod std;
24
28
use io:: WriterUtil ;
25
29
use std:: map:: HashMap ;
@@ -47,7 +51,7 @@ impl cmplx : ops::Add<cmplx,cmplx> {
47
51
}
48
52
}
49
53
50
- struct Line { i : uint , b : ~[ u8 ] }
54
+ type line = { i : uint , b : ~[ u8 ] } ;
51
55
52
56
pure fn cabs ( x : cmplx ) -> f64
53
57
{
@@ -83,7 +87,7 @@ fn fillbyte(x: cmplx, incr: f64) -> u8 {
83
87
rv
84
88
}
85
89
86
- fn chanmb ( i : uint , size : uint , ch : oldcomm:: Chan < Line > ) -> ( )
90
+ fn chanmb ( i : uint , size : uint , ch : oldcomm:: Chan < line > ) -> ( )
87
91
{
88
92
let mut crv = ~[ ] ;
89
93
let incr = 2f64 /( size as f64 ) ;
@@ -93,22 +97,22 @@ fn chanmb(i: uint, size: uint, ch: oldcomm::Chan<Line>) -> ()
93
97
let x = cmplx { re : xincr* ( j as f64 ) - 1.5f64 , im : y} ;
94
98
crv. push ( fillbyte ( x, incr) ) ;
95
99
} ;
96
- oldcomm:: send ( ch, Line { i : i, b : move crv} ) ;
100
+ oldcomm:: send ( ch, { i: i, b: crv} ) ;
97
101
}
98
102
99
103
type devnull = { dn : int } ;
100
104
101
105
impl devnull: io:: Writer {
102
106
fn write ( _b : & [ const u8 ] ) { }
103
- fn seek ( _i : int , _s : io:: SeekStyle ) { }
107
+ fn seek ( + _i : int , + _s : io:: SeekStyle ) { }
104
108
fn tell ( ) -> uint { 0_ u}
105
109
fn flush ( ) -> int { 0 }
106
110
fn get_type ( ) -> io:: WriterType { io:: File }
107
111
}
108
112
109
- fn writer ( path : ~str , writech : oldcomm:: Chan < oldcomm:: Chan < Line > > , size : uint )
113
+ fn writer ( path : ~str , writech : oldcomm:: Chan < oldcomm:: Chan < line > > , size : uint )
110
114
{
111
- let p: oldcomm:: Port < Line > = oldcomm:: Port ( ) ;
115
+ let p: oldcomm:: Port < line > = oldcomm:: Port ( ) ;
112
116
let ch = oldcomm:: Chan ( & p) ;
113
117
oldcomm:: send ( writech, ch) ;
114
118
let cout: io:: Writer = match path {
@@ -160,13 +164,16 @@ fn writer(path: ~str, writech: oldcomm::Chan<oldcomm::Chan<Line>>, size: uint)
160
164
fn main ( ) {
161
165
let args = os:: args ( ) ;
162
166
let args = if os:: getenv ( ~"RUST_BENCH ") . is_some ( ) {
163
- ~[ ~"", ~"4000 "]
167
+ ~[ ~"", ~"4000 ", ~" 10 " ]
164
168
} else {
165
169
args
166
170
} ;
167
171
168
- let path = if vec:: len ( args) < 3_ u { ~"" }
169
- else { copy args[ 2 ] } ; // FIXME: bad for perf
172
+ let path = if vec:: len ( args) < 4_ u { ~"" }
173
+ else { copy args[ 3 ] } ; // FIXME: bad for perf
174
+
175
+ let yieldevery = if vec:: len ( args) < 3_ u { 10_ u }
176
+ else { uint:: from_str ( args[ 2 ] ) . get ( ) } ;
170
177
171
178
let size = if vec:: len ( args) < 2_ u { 80_ u }
172
179
else { uint:: from_str ( args[ 1 ] ) . get ( ) } ;
@@ -178,6 +185,10 @@ fn main() {
178
185
} ;
179
186
let ch = oldcomm:: recv ( writep) ;
180
187
for uint:: range( 0_ u, size) |j| {
181
- do task:: spawn { chanmb( j, size, ch) } ;
188
+ task:: spawn( || chanmb( j, size, ch) ) ;
189
+ if j % yieldevery == 0_ u {
190
+ debug ! ( "Y %u" , j) ;
191
+ task:: yield ( ) ;
192
+ } ;
182
193
} ;
183
194
}
0 commit comments