caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: ygrek <ygrekheretix@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Overhead of first class modules
Date: Fri, 16 Sep 2011 23:36:32 +0300	[thread overview]
Message-ID: <20110916233632.cf8b09af56b83fe3e69a0798@gmail.com> (raw)
In-Reply-To: <CAMB58pGrrz1BLn59i_9eAAMb=Z7D+4dKuoOuydQVfkQ8ODfUFw@mail.gmail.com>

On Fri, 16 Sep 2011 16:08:55 -0400
Alex Rubinsteyn <alex.rubinsteyn@gmail.com> wrote:

> I ran a very simple experiment gauging the overhead of OCaml's new
> first-class modules.

This is not a valid experiment. In first two cases ocamlopt inlines
function call. Also mind that in this code ocamlopt calls the 
Local.f function directly, not through the module.

But let's correctly microbenchmark the cost of direct call vs
indirect vs message sending :)

let n = 100_000_000

let f x = x + 1
let direct () =
  let acc = ref 0 in
  for i = 0 to n do
    acc := !acc + (f 1)
  done

module type S = sig val f : int -> int end
module M = struct let f x = x + 1 end
let package = ref (module M : S)
let fstcls () =
  let acc = ref 0 in
  for i = 0 to n do
     let module Local = (val !package : S) in
     acc := !acc + (Local.f 1)
  done

let o = object method f x = x + 1 end
let obj () =
  let acc = ref 0 in
  for i = 0 to n do
     acc := !acc + o#f 1
  done

let () = 
  match Sys.argv with
  | [|_;"direct"|] -> direct ()
  | [|_;"fstcls"|] -> fstcls ()
  | [|_;"obj"|] -> obj ();
  | _ -> failwith "ORLY?"

Disable inlining :

 /opt/ocaml-3.12.1/bin/ocamlopt -inline 0 -S speed.ml -o speed

Run (Linux 3.0.0-1-686-pae i686) :

$ time ./speed direct

real	0m0.711s
user	0m0.560s
sys	0m0.012s

$ time ./speed fstcls

real	0m0.631s
user	0m0.508s
sys	0m0.000s

$ time ./speed obj

real	0m1.157s
user	0m0.840s
sys	0m0.016s

Looks like it is time to sprinkle some performance-critical code with first class modules :)

-- 
 ygrek
 http://ygrek.org.ua 

      reply	other threads:[~2011-09-16 20:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-16 20:08 Alex Rubinsteyn
2011-09-16 20:36 ` ygrek [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=20110916233632.cf8b09af56b83fe3e69a0798@gmail.com \
    --to=ygrekheretix@gmail.com \
    --cc=caml-list@inria.fr \
    /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;
as well as URLs for NNTP newsgroup(s).