| 1 |
(* From the SML/NJ benchmark suite. *) |
| 2 |
|
| 3 |
signature BMARK = |
| 4 |
sig |
| 5 |
val doit : int -> unit |
| 6 |
val testit : TextIO.outstream -> unit |
| 7 |
end; |
| 8 |
(* mandelbrot.sml *) |
| 9 |
|
| 10 |
structure Main : BMARK = |
| 11 |
struct |
| 12 |
val x_base = ~2.0 |
| 13 |
val y_base = 1.25 |
| 14 |
val side = 2.5 |
| 15 |
|
| 16 |
val sz = 32768 |
| 17 |
val maxCount = 2048 |
| 18 |
|
| 19 |
val delta = side / (real sz) |
| 20 |
|
| 21 |
val sum_iterations = ref 0 |
| 22 |
|
| 23 |
fun loop1 i = if (i >= sz) |
| 24 |
then () |
| 25 |
else let |
| 26 |
val c_im : real = y_base - (delta * real i) |
| 27 |
fun loop2 j = if (j >= sz) |
| 28 |
then () |
| 29 |
else let |
| 30 |
val c_re = x_base * (delta + real j) |
| 31 |
fun loop3 (count, z_re : real, z_im : real) = if (count < maxCount) |
| 32 |
then let |
| 33 |
val z_re_sq = z_re * z_re |
| 34 |
val z_im_sq = z_im * z_im |
| 35 |
in |
| 36 |
if ((z_re_sq + z_im_sq) > 4.0) |
| 37 |
then count |
| 38 |
else let |
| 39 |
val z_re_im = (z_re * z_im) |
| 40 |
in |
| 41 |
loop3 (count+1, |
| 42 |
(z_re_sq - z_im_sq) + c_re, |
| 43 |
z_re_im + z_re_im + c_im) |
| 44 |
end |
| 45 |
end (* loop3 *) |
| 46 |
else count |
| 47 |
val count = loop3 (0, c_re, c_im) |
| 48 |
in |
| 49 |
sum_iterations := !sum_iterations + count; |
| 50 |
loop2 (j+1) |
| 51 |
end |
| 52 |
in |
| 53 |
loop2 0; |
| 54 |
loop1 (i+1) |
| 55 |
end |
| 56 |
|
| 57 |
fun doit () = (sum_iterations := 0; loop1 0) |
| 58 |
|
| 59 |
val doit = |
| 60 |
fn size => |
| 61 |
let |
| 62 |
fun loop n = |
| 63 |
if n = 0 |
| 64 |
then () |
| 65 |
else (doit(); |
| 66 |
loop(n-1)) |
| 67 |
in loop size |
| 68 |
end |
| 69 |
fun testit outstrm = ( |
| 70 |
sum_iterations := 0; |
| 71 |
loop1 0; |
| 72 |
TextIO.output (outstrm, Int.toString(!sum_iterations) ^ " iterations\n")) |
| 73 |
|
| 74 |
end (* Mandelbrot *) |