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 mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id A8F94BBAF for ; Mon, 23 Aug 2010 14:43:10 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AokEACsKckxQW+UMgWdsb2JhbACDF5AOjQsVAQEWIiKnQZFQgSKDInMEiFWBIQ X-IronPort-AV: E=Sophos;i="4.56,257,1280700000"; d="scan'208";a="68106136" Received: from lo.gmane.org ([80.91.229.12]) by mail4-smtp-sop.national.inria.fr with ESMTP; 23 Aug 2010 14:42:47 +0200 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1OnWMP-00043f-J7 for caml-list@inria.fr; Mon, 23 Aug 2010 14:42:45 +0200 Received: from ks368928.kimsufi.com ([94.23.39.26]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 23 Aug 2010 14:42:45 +0200 Received: from sylvain by ks368928.kimsufi.com with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 23 Aug 2010 14:42:45 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Sylvain Le Gall Subject: Re: Question about float refs. Date: Mon, 23 Aug 2010 12:42:36 +0000 (UTC) Message-ID: References: <20100823.140626.634675953541672216.Christophe.Troestler+ocaml@umons.ac.be> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: ks368928.kimsufi.com User-Agent: slrn/pre1.0.0-11 (Linux) 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 byterun:01 burns:98 burns:98 23,:98 wrote:01 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 = ref 0.0 ;; >>> for i = 0 to 1000000000 do r := 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_.  If you rewrite your code as >> >> let r = ref 0.0 in >> for i = 0 to 1000_000_000 do r := 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 0m2.655s > > It does seem to run a lot faster than my first version, but it also > seems to allocate a whole lot. If 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. I think the main reason for the difference is that the first example (float ref not local) implies a call to "caml_modify" (byterun/memory.c|h) which has a cost. This cost is bigger on amd64 architecture because one test is quite expensive (Is_in_heap I think) due to address space randomization. Regards, Sylvain Le Gall