[Shootout-list] OCaml compilation flags, improved Mandelbrot
Christian Szegedy
szegedy at t-online.de
Sun Apr 23 22:54:53 UTC 2006
I have experienced dramatic speedup of the harmonic.ocaml, when
compiled with the command
ocamlop -unsafe -inline 100
The shootout page compiles the code without "-inline 100" which
produces much worse code on my box.
I coud also improve the speed of the Mandelbrot computation.
The following code is about 10-20% faster on my box than
the one in the shootout:
let niter = 50
let limit = 2.
let limit2 = limit *. limit
type complex = {mutable r:float;mutable i:float}
exception Found of int
let add_bit0 tmp c byte =
tmp.r <- c.r;
tmp.i <- c.i;
try
for i = 1 to niter do
if tmp.r *. tmp.r +. tmp.i *. tmp.i > 4.0 then
raise ( Found ((byte lsl 1) lor 0x00));
let m = 2. *. tmp.r *. tmp.i +. c.i in
tmp.r <- tmp.r *. tmp.r -. tmp.i *. tmp.i +. c.r;
tmp.i <- m;
done;
(byte lsl 1) lor 0x01
with
Found i -> i
let () =
let w = int_of_string Sys.argv.(1) in
let h = w in
let fw = float w
and fh = float h
and cplmt8 = 8 - w mod 8 in
Printf.printf "P4\n%i %i\n" w h;
let byte = ref 0
and bit = ref 0 in
let c = {r=0.0;i=0.0} in
let tmp = {r=0.0;i=0.0} in
for y = 0 to h - 1 do
c.i <- 2. *. float y /. fh -. 1.;
for x = 0 to w - 1 do
c.r <- 2. *. float x /. fw -. 1.5;
byte := add_bit0 tmp c !byte;
incr bit;
if !bit = 8 then (
output_byte stdout !byte;
byte := 0;
bit := 0;
)
done;
if !bit <> 0 then (
output_byte stdout (!byte lsl cplmt8);
byte := 0;
bit := 0;
)
done
More information about the Shootout-list
mailing list