From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@sympa.inria.fr Delivered-To: caml-list@sympa.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id A322D7FA15 for ; Sun, 29 Nov 2015 19:13:53 +0100 (CET) IronPort-PHdr: 9a23:loySpBcjUTImwcRv36bRKaEBlGMj4u6mDksu8pMizoh2WeGdxc65bR7h7PlgxGXEQZ/co6odzbGG7uawBydZucfJmUtBWaIPfidNsd8RkQ0kDZzNImzAB9muURYHGt9fXkRu5XCxPBsdMs//Y1rPvi/6tmZKSV3BPAZ4bt74BpTVx5zukbviptuDPU4R3Gv1SIgxBSv1hD2ZjtMRj4pmJ/R54TryiVwMRd5rw3h1L0mYhRf265T41pdi9yNNp6BprJYYAu2pN5g/GLdRCTBjN2Eu+OXqswPCRE2B/CgySGITxz9BGQuNzBjrUpr3+n//v/B+1ymTLMH7S5glUDSl6OFgTxq+23RPDCIw7GyC0p84t6lcuh/0/xE= Authentication-Results: mail3-smtp-sop.national.inria.fr; spf=None smtp.pra=markghayden@yahoo.com; spf=Pass smtp.mailfrom=markghayden@yahoo.com; spf=None smtp.helo=postmaster@nm21-vm4.bullet.mail.ne1.yahoo.com Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of markghayden@yahoo.com) identity=pra; client-ip=98.138.91.181; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="markghayden@yahoo.com"; x-sender="markghayden@yahoo.com"; x-conformance=sidf_compatible Received-SPF: Pass (mail3-smtp-sop.national.inria.fr: domain of markghayden@yahoo.com designates 98.138.91.181 as permitted sender) identity=mailfrom; client-ip=98.138.91.181; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="markghayden@yahoo.com"; x-sender="markghayden@yahoo.com"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: None (mail3-smtp-sop.national.inria.fr: no sender authenticity information available from domain of postmaster@nm21-vm4.bullet.mail.ne1.yahoo.com) identity=helo; client-ip=98.138.91.181; receiver=mail3-smtp-sop.national.inria.fr; envelope-from="markghayden@yahoo.com"; x-sender="postmaster@nm21-vm4.bullet.mail.ne1.yahoo.com"; x-conformance=sidf_compatible X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A0CKAAAGQFtWlLVbimJdgi6BYG8QrGmBT4U4jBwZhXYCgRk7EQEBAQEBAQEBEAEBAQEHCwsJHzCCLYIIAQEEIx0BOQ8GRwICRRKIMQEDEqgSgWqCeYVpASMnA4RJAQEBBwEBAQEBAQEBARQGDAIBiFWIFYJOL4EVBY4YiD+FKogOFIIQhnwgkyE3gjAjgXxThXEBAQE X-IPAS-Result: A0CKAAAGQFtWlLVbimJdgi6BYG8QrGmBT4U4jBwZhXYCgRk7EQEBAQEBAQEBEAEBAQEHCwsJHzCCLYIIAQEEIx0BOQ8GRwICRRKIMQEDEqgSgWqCeYVpASMnA4RJAQEBBwEBAQEBAQEBARQGDAIBiFWIFYJOL4EVBY4YiD+FKogOFIIQhnwgkyE3gjAjgXxThXEBAQE X-IronPort-AV: E=Sophos;i="5.20,360,1444687200"; d="scan'208,217";a="155516330" Received: from nm21-vm4.bullet.mail.ne1.yahoo.com ([98.138.91.181]) by mail3-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 29 Nov 2015 19:13:52 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1448820830; bh=Dem81/jwbYw82t+vTkkUyI5hMhImYxdr9iR8s61P7CI=; h=From:Subject:Date:References:To:In-Reply-To:From:Subject; b=QTJG+5XKFpJtC3JmFH3zkPcj9JRixLPCgc0hi3NobthuKArgE+65gTJNqGB4th8vNwnciB1uCGJ4g/V0sxy0PN1rykVEXxNbBfDll6wZCXPVyb5FB9RAXN9RwvdviV4dBzNofFLnOMoSfvmj+GrJ/wF3X5dui5+rRmSmSNQF1R92JCF23AI41n6PkfxiN1MhMo8VlEYVwTkbIfSyzGAoCsBlM8l94PA0HGrj4QkoRkSW67NEct6bptXewCOWEqePkgt1TCarG9RHCr8eCuT0PBXuUzDsRt0UaY3/Oa5lzK7m7fQF9MknW+QNvd0GDXDiopHK8NoYJOsRSJkRD9mpNw== Received: from [98.138.100.116] by nm21.bullet.mail.ne1.yahoo.com with NNFMP; 29 Nov 2015 18:13:50 -0000 Received: from [98.138.84.45] by tm107.bullet.mail.ne1.yahoo.com with NNFMP; 29 Nov 2015 18:13:50 -0000 Received: from [127.0.0.1] by smtp113.mail.ne1.yahoo.com with NNFMP; 29 Nov 2015 18:13:50 -0000 X-Yahoo-Newman-Id: 140426.7022.bm@smtp113.mail.ne1.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: aAFJUmwVM1kR8qm9DWYCkNBtBjwvMWCOp8Z7y0d44WdXq2_ CJlmV2m.9ZLCtFjM3sQevW0sSawyu5tB4lMksI.YCQlKStusRmaozduonzra P9kLQnYPV6JrsuuPVhNfp_QxA7UWjqd8KPJ2fvLl5thXZY1RtwkZ5JQdecPm 5ylKAut749Ysns2DVKhA.hpkxbyp3flIZ8Vo1K7Ld2S44M7IkxlyPJOQPnIT YiA_1v4fPqQt9JWKp2Y9aYa80ZJJB94Txcec9qV2000jisbXyp69DT2qNTC2 bDFw0eYswkQ7ez9ihqpGZugU6yVD_5MQRLVD9zoIJrRmD8J9Fr8CMfYwC41E sN5FKXiVbd9Wj6qc2CtLe2TQhmP7gbbqPlFtp41w2tkDa0gUXDCf08EbLT8u aFPxSRoY4EniNQfTcXkfdDQlAHfZEs5Dk1PCl_NXVckxgByNRqjrkeDM_5Hk LnwJSNGLPgd3V45_47SwiKlS_Nm3Up5DVp2w3LyaVLVW8Jh4kDvH5MSlHf9S s6FyBnutLO_4lUQ.4TwCcqqMleEc3XIP8a6QCT1kVlCGU3Jk- X-Yahoo-SMTP: 1rhWOp.swBCIsRPqec.N67IhngFT7HDF From: Mark Hayden Content-Type: multipart/alternative; boundary="Apple-Mail=_2E6763B8-0E61-4C6C-8350-A6A217AE204A" Message-Id: <887BAE10-E073-40B2-8607-BE5444CBA2FC@yahoo.com> Mime-Version: 1.0 (Mac OS X Mail 9.1 \(3096.5\)) Date: Sun, 29 Nov 2015 12:16:57 -0600 References: <20151129110020.D92E17FD40@sympa.inria.fr> To: caml-list@inria.fr In-Reply-To: <20151129110020.D92E17FD40@sympa.inria.fr> X-Mailer: Apple Mail (2.3096.5) Subject: [Caml-list] OCaml optimization --Apple-Mail=_2E6763B8-0E61-4C6C-8350-A6A217AE204A Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Below are two ways to write array summation in OCaml, sum0 and sum1. Both = of them use recursive functions. =E2=80=99sum0' allocates a closure on eac= h call but I think is more natural. =E2=80=98sum1=E2=80=99 does do any all= ocation, 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 =E2=80=98sum0=E2=80=99? best, Mark Running each function 1000 times show sum0 is allocating 6 words on each ca= ll. % ocamlopt -o loop0 -S -inline 10 -unsafe loop0.ml ./loop0 sum0:6029 sum1:23 open Printf ;; (* Allocates *) let sum0 a =3D let len =3D Array.length a in let rec loop ofs sum =3D if ofs < len then ( loop (succ ofs) (sum + a.(ofs)) ) else ( sum ) in loop 0 0 ;; let rec sum1_help a len ofs sum =3D if ofs < len then ( sum1_help a len (succ ofs) (sum + a.(ofs)) ) else ( sum ) ;; (* Does not allocate *) let sum1 a =3D let len =3D Array.length a in sum1_help a len 0 0 ;; let test which sum =3D let a =3D Array.make 123 123 in let minor0 =3D (Gc.stat ()).Gc.minor_words in =20=20 for ofs =3D 0 to 1000 do ignore (sum a) done ; let minor1 =3D (Gc.stat ()).Gc.minor_words in printf "%s:%.0f\n" which (minor1 -. minor0) ; ;; let _ =3D test "sum0" sum0 ; test "sum1" sum1 ; ;; --Apple-Mail=_2E6763B8-0E61-4C6C-8350-A6A217AE204A Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 Below are two ways to write a= rray summation in OCaml, sum0 and sum1.  Both of them use recursive fu= nctions.  =E2=80=99sum0' allocates a closure on each call but I think = is more natural.  =E2=80=98sum1=E2=80=99 does do any allocation, but i= s 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 =E2=80=98sum0=E2=80=99?

best, Mark


Running each function 1000 ti= mes 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 =3D
=   let len =3D Array.length a in
  let rec loop ofs sum =3D
    if ofs < len then (=
    =   loop (succ ofs) (sum + a.(ofs))
    ) else (
=       sum
    )
  in
  loop 0 0
;;


let rec sum1_help a len ofs sum =3D
  if ofs < len th= en (
  &nb= sp; sum1_help a len (succ ofs) (sum + a.(ofs))
=   ) else (
    sum
  )
= ;;

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


<= div class=3D"">let test which sum =3D
  let a =3D Arr= ay.make 123 123 in

  let minor0 =3D (Gc.stat ()).Gc.minor_words in
  
  for ofs =3D 0 to 1000 do<= /font>
    i= gnore (sum a)
&= nbsp; done ;
&= nbsp; let minor1 =3D (Gc.stat ()).Gc.minor_words in

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

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



<= br class=3D"">

= --Apple-Mail=_2E6763B8-0E61-4C6C-8350-A6A217AE204A--