From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p06F5dBe012269 for ; Thu, 6 Jan 2011 16:05:39 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AiQBAK9oJU0machzl2dsb2JhbACkLgEBAQEBCBUHNb0yhUwEhGc X-IronPort-AV: E=Sophos;i="4.60,283,1291590000"; d="scan'208";a="86182875" Received: from mx2.janestreet.com (HELO nyc-dmz-mxout2.janestreet.com) ([38.105.200.115]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 06 Jan 2011 16:05:34 +0100 Received: from nyc-smtp.janestreet.com ([172.25.22.57] helo=nyc-qsv-mail1.delacy.com) by nyc-dmz-mxout2.janestreet.com with esmtp (Exim 4.71) (envelope-from ) id 1ParPA-000381-Ao for caml-list@inria.fr; Thu, 06 Jan 2011 10:05:32 -0500 Received: from nyc-qws-062.delacy.com ([172.25.135.162]) by nyc-qsv-mail1.delacy.com with esmtpsa (SSLv3:AES256-SHA:256) (Exim 4.71) (envelope-from ) id 1ParPA-0005yy-95; Thu, 06 Jan 2011 10:05:32 -0500 From: Pascal Zimmer To: Jim Pryor Cc: "caml-list@inria.fr" In-Reply-To: <20110106144406.GD12229@vaio.hsd1.pa.comcast.net> References: <20110106130539.GB12229@vaio.hsd1.pa.comcast.net> <20110106144406.GD12229@vaio.hsd1.pa.comcast.net> Content-Type: text/plain Date: Thu, 06 Jan 2011 10:05:32 -0500 Message-Id: <1294326332.17561.3.camel@nyc-qws-062.delacy.com> Mime-Version: 1.0 X-Mailer: Evolution 2.12.3 (2.12.3-19.el5) Content-Transfer-Encoding: 7bit X-JS-Compliance: sender=pzimmer recipient=caml-list (ld) Subject: Re: [Caml-list] memory leak in toplevel? or, how to implement sizeof Because the overhead parameter can vary based on which version of OCaml you are running, what the Gc module does, etc, I usually implement this kind of measurements in this way: # let sizeof maker arg = let first = Gc.((stat()).minor_words) in for i = 1 to 1000000 do ignore (maker arg) done; let second = Gc.((stat()).minor_words) in (second -. first) /. 1e6;; # sizeof (fun x -> [x]) 1;; - : float = 3.000022 The overhead gets amortized enough that is becomes negligible (you only have to rely on the fact that the loop does not do any allocation). Pascal On Thu, 2011-01-06 at 09:44 -0500, Jim Pryor wrote: > Thanks David. So does this look like a reasonable implementation of > sizeof: > > # let sizeof maker arg = > let first = Gc.((stat()).minor_words) in > let res = maker arg in > let second = Gc.((stat()).minor_words) in > int_of_float (second -. first) - 23 ( *overhead *), res;; > > # sizeof (fun x -> [x]) 1;; > - : int * int list = (3, [1]) > > # sizeof (fun x -> Some x) 1;; > - : int * int option = (2, Some 1) > > It does give the right answers. ([1] is a block containing two non-blocks; Some 1 is a block containing one non-block.) > > > -- > Jim Pryor > profjim@jimpryor.net >