Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Pascal Zimmer <pzimmer@janestreet.com>
To: Jim Pryor <lists+caml@jimpryor.net>
Cc: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: Re: [Caml-list] memory leak in toplevel? or, how to implement sizeof
Date: Thu, 06 Jan 2011 10:05:32 -0500	[thread overview]
Message-ID: <1294326332.17561.3.camel@nyc-qws-062.delacy.com> (raw)
In-Reply-To: <20110106144406.GD12229@vaio.hsd1.pa.comcast.net>

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
> 


      parent reply	other threads:[~2011-01-06 15:05 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-01-06 13:05 Jim Pryor
2011-01-06 14:20 ` David Allsopp
2011-01-06 14:44   ` Jim Pryor
2011-01-06 14:59     ` Jim Pryor
2011-01-07 12:25       ` Damien Doligez
2011-01-07 14:14         ` Jim Pryor
2011-01-07 14:22           ` dmitry grebeniuk
2011-01-06 15:05     ` Pascal Zimmer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1294326332.17561.3.camel@nyc-qws-062.delacy.com \
    --to=pzimmer@janestreet.com \
    --cc=caml-list@inria.fr \
    --cc=lists+caml@jimpryor.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox