Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Interesting optimization
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Daniel_Bünzli <daniel.buenzli@e...>
Subject: [Caml-list] Interesting optimization

I usually try not to be too much obsessed with speed, but I had the 
following interesting experience. While rearanging some checksum code I 
thought that I had rewritten it in a more efficient way. However I 
turned out not to be the case.

I can boil the example to the following (n.b. loops don't compute 
anything usefull). Basically I rewrote update into update'.

--- ---
let update c =
   let c' = ref !c in
   for n = 0 to max_int do
     c' := !c' land 0xff
   c := !c'

let update' c =
   for n = 0 to max_int do
     c := !c land 0xff;

let compute use_ref =
   let x = ref 2 in
   if use_ref then update' x else update x;
   print_int !x

let main () =
   let use_ref = ref false in
   let args = [("-ref", (Arg.Set use_ref), "use reference directly")] in
   Arg.parse args (fun _ -> ()) "";
   compute !use_ref

let () = main ()

 > ocamlopt -o test.opt
 > time ./test.opt
real    0m3.500s
user    0m3.230s
sys     0m0.010s
 > time ./test.opt -ref
real    0m7.599s
user    0m7.550s
sys     0m0.030s

The few that I can read of ppc assembly tells me that in update the 
value of c' is directly stored in a register whearas update' accesses 
memory on each iteration.

Note that this is not restricted to int's, it occured to me with an 
int32. I guess it should work with anything that gets into a register.



To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: