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 XAA25907; Wed, 7 Jul 2004 23:28:53 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id XAA25512 for ; Wed, 7 Jul 2004 23:28:52 +0200 (MET DST) Received: from ptb-relay01.plus.net (ptb-relay01.plus.net [212.159.14.212]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i67LSpEV019009 for ; Wed, 7 Jul 2004 23:28:51 +0200 Received: from [80.229.56.224] (helo=chetara) by ptb-relay01.plus.net with esmtp (Exim) id 1BiJyE-0006Gm-W2 for caml-list@inria.fr; Wed, 07 Jul 2004 21:28:51 +0000 From: Jon Harrop To: caml-list@inria.fr Subject: Re: [Caml-list] Does Caml have slow arithmetics ? Date: Wed, 7 Jul 2004 22:26:36 +0100 User-Agent: KMail/1.6.2 References: <20040707145803.GB27498@yquem.inria.fr> In-Reply-To: <20040707145803.GB27498@yquem.inria.fr> MIME-Version: 1.0 Content-Disposition: inline Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Message-Id: <200407072226.36912.postmaster@jdh30.plus.com> X-Miltered: at nez-perce with ID 40EC6B13.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; postmaster:99 caml-list:01 arithmetics:01 2004:99 ocamlopt's:01 low-level:01 terrible:01 ocamlopt:01 optimise:01 pivot:01 helper:01 pivot:01 helper:01 slower:01 ocamlopt:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Wednesday 07 July 2004 15:58, Xavier Leroy wrote: > ... In your "f" function, you asked for the > construction of a pair, and you get that pair construction in > ocamlopt's output. If you didn't want the pair to be constructed, why > did you write "f" this way? =46unny you should say that. I've only recently been trying my hand at such= =20 low-level optimisations in OCaml and, to my surprise, I'm terrible at it (n= o=20 comments, please!). Specifically, I recalled a post that ocamlopt doesn't do CSE. On the basis = of=20 this I thought that I could optimise my original code: let pivot =3D (first + last)/2 in let sum1=3Dhelper (scale+1) (2*entry) first pivot in let sum2=3Dhelper (scale+1) (2*entry+1) pivot last in Array.unsafe_set data2 entry (sum1 -. sum2); sum1 +. sum2 by performing some CSE manually, for example: let pivot =3D (first + last)/2 in let (sum1, sum2) =3D let scale=3Dscale+1 and entry=3D2*entry in (helper scale entry first pivot, helper scale (entry+1) pivot last) in Array.unsafe_set data2 entry (sum1 -. sum2); sum1 +. sum2 I also tried applying part of the arguments to Array.set to avoid the creat= ion=20 of a pair: let pivot =3D (first + last)/2 in let set=3DArray.unsafe_set data2 entry in let scale=3Dscale+1 and entry=3D2*entry in let sum1=3Dhelper scale entry first pivot in let sum2=3Dhelper scale (entry+1) pivot last in set (sum1 -. sum2); sum1 +. sum2 Contrary to my expectations, these revised versions are 30 and 70% slower t= han=20 the original, respectively. In the first case, this is presumably because=20 ocamlopt is building the pair which I asked for but don't want. In the seco= nd=20 case, "set" appears to be very expensive to create and/or apply (forgive my= =20 avoiding the word "closure" - I'm still not confident that I know its preci= se=20 meaning). So, in order to optimise OCaml code in this much detail, I guess I need som= e=20 quantifications of the relative costs of such "primitive" operations. Has=20 anyone documented this and can anyone give me some hints? =46rom Evgeny Chukreev's post I'm guessing that constructing any data=20 structures, even pairs, can be very costly because it invokes the GC which= =20 (always?) does a minor collection. Can partially applied functions be more= =20 efficient if they are applied many times? (For anyone who's interested, bounds checking only slows this array-intensi= ve=20 code down by 9%) Cheers, Jon. ------------------- 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