caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Mark Hayden <markghayden@yahoo.com>
To: caml-list@inria.fr
Subject: [Caml-list] OCaml optimization
Date: Sun, 29 Nov 2015 12:16:57 -0600	[thread overview]
Message-ID: <887BAE10-E073-40B2-8607-BE5444CBA2FC@yahoo.com> (raw)
In-Reply-To: <20151129110020.D92E17FD40@sympa.inria.fr>

[-- Attachment #1: Type: text/plain, Size: 1379 bytes --]

Below are two ways to write array summation in OCaml, sum0 and sum1.  Both of them use recursive functions.  ’sum0' allocates a closure on each call but I think is more natural.  ‘sum1’ does do any allocation, but is awkward to write.  Is there a compiler flag or other way to get the OCaml native compiler to avoid closure allocation in functions in the form of ‘sum0’?

best, Mark


Running each function 1000 times show sum0 is allocating 6 words on each call.

% ocamlopt -o loop0 -S -inline 10 -unsafe loop0.ml
./loop0
sum0:6029
sum1:23



open Printf ;;

(* Allocates
 *)
let sum0 a =
  let len = Array.length a in
  let rec loop ofs sum =
    if ofs < len then (
      loop (succ ofs) (sum + a.(ofs))
    ) else (
      sum
    )
  in
  loop 0 0
;;


let rec sum1_help a len ofs sum =
  if ofs < len then (
    sum1_help a len (succ ofs) (sum + a.(ofs))
  ) else (
    sum
  )
;;

(* Does not allocate
 *)
let sum1 a =
  let len = Array.length a in
  sum1_help a len 0 0
;;


let test which sum =
  let a = Array.make 123 123 in

  let minor0 = (Gc.stat ()).Gc.minor_words in
  
  for ofs = 0 to 1000 do
    ignore (sum a)
  done ;

  let minor1 = (Gc.stat ()).Gc.minor_words in

  printf "%s:%.0f\n" which (minor1 -. minor0) ;
;;

let _ =
  test "sum0" sum0 ;
  test "sum1" sum1 ;
;;







[-- Attachment #2: Type: text/html, Size: 5266 bytes --]

       reply	other threads:[~2015-11-29 18:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20151129110020.D92E17FD40@sympa.inria.fr>
2015-11-29 18:16 ` Mark Hayden [this message]
2015-11-29 23:28   ` Nicolas Ojeda Bar
2015-11-30  9:53   ` Mark Shinwell

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=887BAE10-E073-40B2-8607-BE5444CBA2FC@yahoo.com \
    --to=markghayden@yahoo.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).