From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id XAA32536; Thu, 22 Jul 2004 23:05:25 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id XAA00408 for ; Thu, 22 Jul 2004 23:05:24 +0200 (MET DST) Received: from mail3.bluewin.ch (mail3.bluewin.ch [195.186.1.75]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i6ML5NSH015355 for ; Thu, 22 Jul 2004 23:05:23 +0200 Received: from [10.0.1.2] (81.62.93.107) by mail3.bluewin.ch (Bluewin AG 7.0.030.2) id 40ED9D270019FD59 for caml-list@inria.fr; Thu, 22 Jul 2004 21:05:22 +0000 Mime-Version: 1.0 (Apple Message framework v618) Content-Transfer-Encoding: 7bit Message-Id: <146D181C-DC23-11D8-8ADE-000393DBC266@epfl.ch> Content-Type: text/plain; charset=US-ASCII; format=flowed To: caml-list@inria.fr From: =?ISO-8859-1?Q?Daniel_B=FCnzli?= Subject: [Caml-list] Interesting optimization Date: Thu, 22 Jul 2004 23:07:03 +0200 X-Mailer: Apple Mail (2.618) X-Miltered: at concorde with ID 41002C13.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; obsessed:01 checksum:01 rewrote:01 ocamlopt:01 0.010:01 int's:01 int:01 int:01 rewritten:02 sys:03 sys:03 usefull:03 let:04 let:04 parse:04 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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