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 mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by sympa.inria.fr (Postfix) with ESMTPS id 10FA07ED26 for ; Fri, 8 Jun 2012 00:11:07 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AsACALkl0U9dEYBRk2dsb2JhbABFixCpPSIBAQEBCQkLCRQDJIITBQEBBQgBAR0LAQUfASwBAQMFAgQNBAQBAQolFAEECBIcCAYTCAIBAgMBCodiAw+vSQ2JToo9YIJngjlgA409kl6HXg X-IronPort-AV: E=Sophos;i="4.75,732,1330902000"; d="scan'208";a="146963247" Received: from smtp24.services.sfr.fr ([93.17.128.81]) by mail4-smtp-sop.national.inria.fr with ESMTP; 08 Jun 2012 00:11:06 +0200 Received: from filter.sfr.fr (localhost [127.0.0.1]) by msfrf2404.sfr.fr (SMTP Server) with ESMTP id 1BCBC70000E1 for ; Fri, 8 Jun 2012 00:11:06 +0200 (CEST) Received: from Ganymede (117.171.13.109.rev.sfr.net [109.13.171.117]) by msfrf2404.sfr.fr (SMTP Server) with SMTP id BE63D70000BC for ; Fri, 8 Jun 2012 00:11:05 +0200 (CEST) X-SFR-UUID: 20120607221105779.BE63D70000BC@msfrf2404.sfr.fr Message-ID: From: "Alain Coste" To: References: <1339099794.4950.8@samsung> Date: Fri, 8 Jun 2012 00:10:59 +0200 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-15"; reply-type=response Content-Transfer-Encoding: 8bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.5931 X-Mimeole: Produced By Microsoft MimeOLE V6.00.2900.6109 X-Antivirus: avast! (VPS 120607-0, 07/06/2012), Outbound message X-Antivirus-Status: Clean X-Validation-by: alaincoste@club-internet.fr Subject: Re: [Caml-list] Local functions with arguments or closures ? Hello, This question recalls me a related one I am often asking myself when writing large functions (say 50 to 100 lines) which use several auxilliary functions sharing the same parameters. For example let f x = let g1 () = .. reference to x ... in ....... let gn () = .... reference to x .... in calls to to g1() ... gn() I would like to have an idea of the cost of putting the code of g1...gn "inside" f rather than defining them separately and passing x as a parameter, or using a reference With x as parameter: let g1 x = ..... .... let gn x = .... let f x = calls to g1 x .... gn x Or with a reference: let f = let xr = ref 0 in let g1 () = ...!xr ... in .... let gn () = ... !xr... in fun x -> xr := x; .. calls to g1()...gn() Some years ago I had the same problem with an interpreted language, and the answer was clearly in favor of the second or third solution, as the body of the function f was evaluated only when the function was applied (and as many times it was applied). But it seems that with a compiler things are different as the body of the large function is treated at compile time, and only once. So I suppose the extra cost is only to build a closure, but does not depend on the size of the body of the large function f. I generally use the first solution, but I sometimes have some doubts concerning efficiency... Alain Coste ----- Original Message ----- From: "Gerd Stolpmann" To: "Gabriel Scherer" Cc: "Daniel Bünzli" ; "caml-list" Sent: Thursday, June 07, 2012 10:09 PM Subject: AW: [Caml-list] Local functions with arguments or closures ? I think it depends very much would you do inside your loop. In Gabriel's example, there is only an addition, so you will probably see a difference in runtime. But that's not what you do in practice here. Imagine the loop iterates over a data structure, and let's assume it is big enough that it is not completely cached. Then, the few extra register loads will make no difference, they are just hidden by the speculative execution done by modern CPUs (i.e. the loads are done while the CPU waits for the data from RAM). So yes, passing the arguments explicitly is probably only worth for extremely simple cases that do not access data massively, but otherwise use the style you like. Gerd