From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 4E981BBAF for ; Mon, 23 Aug 2010 15:00:10 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AhQDACcOckzRVdivkGdsb2JhbACTJY0ACBUBAQEBCQkMBxEDH58FiRiCE4YALohUAQEDBYUyBIQwhUY X-IronPort-AV: E=Sophos;i="4.56,257,1280700000"; d="scan'208";a="57686630" Received: from mail-qy0-f175.google.com ([209.85.216.175]) by mail2-smtp-roc.national.inria.fr with ESMTP; 23 Aug 2010 15:00:04 +0200 Received: by qyk8 with SMTP id 8so2803576qyk.6 for ; Mon, 23 Aug 2010 06:00:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=FAPAUWyMZpfuPBYN9WJrRzHPTYz5frk4q8Wp1nFe4PE=; b=tLNdggLJ2xCVxdgWmHPOdNvbyM/r8TTcSwJN2L6vz852Zg/wFedhyMADcJ3xgJcxY1 1tKGG59nkNOLOLc1JJobuhRhYWzb0k/VT1xSS4NbTr5amEFLuRM5O14ak8EV+c0g+93T vRCrcgw7eb84Lmhhxx2vMNlrifAQpPLxWzFPA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=XfpiqNltz0U1kxMy9v0pMRywoOdsDeYn3UoHMPw3W4836aS/lPYor9D9VGPlB9rCZa /3BjfcK8Ysuhy1dDWXbUAVcsKAw7T37F2G38sY79c6rzVPhMCVlQKBfFLEnNHGOx8AUN J5D75DCxjxIv+wfLTFv0z/l/o0oa7cLolrRKg= MIME-Version: 1.0 Received: by 10.229.189.83 with SMTP id dd19mr3564015qcb.92.1282568403911; Mon, 23 Aug 2010 06:00:03 -0700 (PDT) Received: by 10.229.233.15 with HTTP; Mon, 23 Aug 2010 06:00:03 -0700 (PDT) In-Reply-To: References: <20100823.140626.634675953541672216.Christophe.Troestler+ocaml@umons.ac.be> Date: Mon, 23 Aug 2010 17:00:03 +0400 Message-ID: Subject: Re: [Caml-list] Re: Question about float refs. From: Dmitry Bely To: caml-list@inria.fr Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Spam: no; 0.00; le-gall:01 christophe:01 troestler:01 christophe:01 troestler:01 ocaml:01 printf:01 printf:01 unboxed:01 allocating:01 23,:98 burns:98 burns:98 23,:98 wrote:01 On Mon, Aug 23, 2010 at 4:42 PM, Sylvain Le Gall wrot= e: > On 23-08-2010, Ethan Burns wrote: >> On Mon, Aug 23, 2010 at 8:06 AM, Christophe TROESTLER >> wrote: >>> On Thu, 19 Aug 2010 07:52:33 -0400, Ethan Burns wrote: >>>> >>>> let r =3D ref 0.0 ;; >>>> for i =3D 0 to 1000000000 do r :=3D float i done; >>>> Printf.printf "%f\n" !r; >>>> Printf.printf "words: %f\n" (Gc.stat ()).Gc.minor_words >>> >>> To add a precision to others' answers : float refs are unboxed >>> _locally_. =A0If you rewrite your code as >>> >>> let r =3D ref 0.0 in >>> for i =3D 0 to 1000_000_000 do r :=3D float i done; >>> Printf.printf "%f\n" !r; >>> Printf.printf "words: %f\n" (Gc.stat ()).Gc.minor_words >>> >>> then it runs at about the same speed as you other version. >> >> >> $ time ./a.out >> 1000000000.000000 >> words: 2000000367.000000 >> >> real =A00m2.655s >> >> It does seem to run a lot faster than my first version, but it also >> seems to allocate a whole lot. =A0If it is still allocating just as much >> why is this version so much faster? >> > > Allocation on the minor heap is very cheap compared to assignement into > the major heap. It is better to allocate a lot on the minor heap than to > do operations on the major heap. Some black magic is needed: let r =3D ref 0.0 in for i =3D 0 to 1000_000_000 do r :=3D float i done; Printf.printf "%f\n" (!r +. 0.); Printf.printf "words: %f\n" (Gc.stat ()).Gc.minor_words and the reference will be placed in a register (Caml heap will not be used inside the loop) - Dmitry Bely