Version française
Home     About     Download     Resources     Contact us    
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
Hello,

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'.

--- test.ml ---
let update c =
   let c' = ref !c in
   for n = 0 to max_int do
     c' := !c' land 0xff
   done;
   c := !c'

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

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 test.ml
 > time ./test.opt
2
real    0m3.500s
user    0m3.230s
sys     0m0.010s
 > time ./test.opt -ref
2
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.

Cheers,

Daniel

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners