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 mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 444B1BC57 for ; Thu, 19 Aug 2010 13:52:35 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgUCAEW4bEzRVdQ0kGdsb2JhbACTQY0TCBUBAQEBCQkMBxEDH6EliRiCE4ZELohUAQEDBYUyBIlx X-IronPort-AV: E=Sophos;i="4.56,233,1280700000"; d="scan'208";a="65869363" Received: from mail-vw0-f52.google.com ([209.85.212.52]) by mail1-smtp-roc.national.inria.fr with ESMTP; 19 Aug 2010 13:52:34 +0200 Received: by vws14 with SMTP id 14so1867057vws.39 for ; Thu, 19 Aug 2010 04:52:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:date:message-id :subject:from:to:cc:content-type; bh=7LQFw+Lluy496DILrQWd3uQdSYByJFHsHlhw8ZBp//w=; b=Y20wSH145DNSJGPbaacWan4hAEQdjqpEHpTCpsS9l0UfatCAu4fkYW2cBQ1sG4Pj3O FBDijTlxU1fAKSJuCcL2kGKqwmOCzNR3N9F37BpogDBDPifPC/2QmIQiItsxTA+c9Uad PB/JhBpKG7Pco+yVjtSpsJl31fbvOLEhSPEvg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:cc:content-type; b=gosqNrkB7lxTjXgBVbBzLhAfw75YoBB8MXU1A+QVnzj0Zy/7x/PajVuN1KY1ivS0kw DioFB1/jONqWWEKIpibp+kaZ/gqpgCSS+tvXI0lSh/d6BMJj5gi4EXPrTaIq/gBy4Te0 njzoEkf2R/feRrp7IrWvkzfidhayiIy22W7Yc= MIME-Version: 1.0 Received: by 10.220.122.103 with SMTP id k39mr2645418vcr.207.1282218753823; Thu, 19 Aug 2010 04:52:33 -0700 (PDT) Received: by 10.220.179.1 with HTTP; Thu, 19 Aug 2010 04:52:33 -0700 (PDT) Date: Thu, 19 Aug 2010 07:52:33 -0400 Message-ID: Subject: Question about float refs. From: Ethan Burns To: caml-list@yquem.inria.fr Cc: Wheeler Ruml Content-Type: text/plain; charset=ISO-8859-1 X-Spam: no; 0.00; mutable:01 ocaml:01 printf:01 printf:01 allocations:01 mutable:01 allocates:01 burns:98 burns:98 quicker:98 minor:01 minor:01 data:02 data:02 seems:03 Hello, I noticed a strange behavior when using references to floating point values. My suspicion, initially, was that the references would not be boxed because they are the same as a record with a single mutable field that is of type float. It seems that OCaml still boxes float refs. Consider the following two pieces of code: 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 --------------- $ time ./a.out 1000000000.000000 words: 2000000373.000000 10.073 secs This seems to perform a lot of allocations and it certainly takes a long time to run. But, now, if we use a record instead: type float_ref = { mutable data : float } let r = { data = 0.0 } ;; for i = 0 to 1000000000 do r.data <- float i done; Printf.printf "%f\n" r.data; Printf.printf "words: %f\n" (Gc.stat ()).Gc.minor_words --------------- $ time ./a.out 1000000000.000000 words: 373.000000 0.959 secs It runs *much* quicker and hardly allocates anything. My question is: is there something better than creating a special float_ref type every time I would like a reference to a floating point number? Typically I want a reference to something because it is going to change a lot in a loop and then be accessed after the loop is complete so it is undesirable for an allocation to occur each time the reference changes. Thanks in advance for any help, Ethan