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 XAA01326; Thu, 22 Jul 2004 23:40:34 +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 XAA01686 for ; Thu, 22 Jul 2004 23:40:33 +0200 (MET DST) Received: from mproxy.gmail.com (rproxy.gmail.com [64.233.170.204]) by concorde.inria.fr (8.12.10/8.12.10) with SMTP id i6MLeWSH019532 for ; Thu, 22 Jul 2004 23:40:32 +0200 Received: by mproxy.gmail.com with SMTP id d19so77079rnf for ; Thu, 22 Jul 2004 14:40:31 -0700 (PDT) Received: by 10.38.92.74 with SMTP id p74mr202672rnb; Thu, 22 Jul 2004 14:40:31 -0700 (PDT) Message-ID: Date: Thu, 22 Jul 2004 17:40:31 -0400 From: John Prevost To: Ocaml Mailing List Subject: Re: [Caml-list] Interesting optimization In-Reply-To: <146D181C-DC23-11D8-8ADE-000393DBC266@epfl.ch> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable References: <146D181C-DC23-11D8-8ADE-000393DBC266@epfl.ch> X-Miltered: at concorde with ID 41003450.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; prevost:01 prevost:01 caml-list:01 2004:99 obsessed:01 checksum:01 rewrote:01 tail-call:01 avoids:01 0.010:01 0.000:01 int:01 rec:01 0200,:01 rewritten:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Thu, 22 Jul 2004 23:07:03 +0200, Daniel B=FCnzli wrote: > 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. >=20 > I can boil the example to the following (n.b. loops don't compute > anything usefull). Basically I rewrote update into update'. Here's a slight addition to your comparison, which may or may not be of interest to you. I added this function: let update'' c =3D let rec loop c' a =3D if a >=3D 0 then loop (c' land 0xff) (succ a) else c' in c :=3D (loop !c 0) Basically, the same thing as your update, only using a tail-recursive loop with an argument accumulator instead of using a for loop with a reference accumulator. Note that I had to dodge a little on the loop end condition, since a will never be > max_int, which would be the normal test for the end of the loop. While I'll pretty much always prefer the tail-call version (which avoids references altogether), the fact that the reference has so little impact when used in the way you described is very nice to know. using update: real 0m9.707s user 0m9.710s sys 0m0.010s using update': real 0m16.172s user 0m16.140s sys 0m0.030s using update'': real 0m8.321s user 0m8.320s sys 0m0.000s John. ------------------- 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