caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Interesting optimization
@ 2004-07-22 21:07 Daniel Bünzli
  2004-07-22 21:40 ` John Prevost
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Bünzli @ 2004-07-22 21:07 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-07-23  1:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-07-22 21:07 [Caml-list] Interesting optimization Daniel Bünzli
2004-07-22 21:40 ` John Prevost
2004-07-23  0:10   ` Brian Hurt
2004-07-23  1:22     ` John Prevost

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).