From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q0KDxinw007868 for ; Fri, 20 Jan 2012 14:59:44 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgECABRyGU/RVda2kGdsb2JhbABDgw2Bd6h1CCIBAQEBCQkNBxQEIYFyAQEBBBICDwQRCAEbHAIDDAYFCw0CAgUWCwICCQMCAQIBEREBBQEcEwgBARcHoygKiyJIgm+Edj+IcQIFC4EkiWGBFgSIPIxdhVWBN4cEPYQc X-IronPort-AV: E=Sophos;i="4.71,542,1320620400"; d="scan'208";a="140741033" Received: from mail-tul01m020-f182.google.com ([209.85.214.182]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/RC4-SHA; 20 Jan 2012 14:59:39 +0100 Received: by obcwo16 with SMTP id wo16so1371047obc.27 for ; Fri, 20 Jan 2012 05:59:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=yznGHDSinkH+xu2KOYYiO83ZPM3MB235rO8MQF2/sHc=; b=YKUr44rKSi5d9zXDCyoZ1uGeOJr3BvnKuttVYbBeTQp7SGNhjRQ/sOtp4puECwWL9R cjhYsPyTRBI9wyDgW9uVeakUiHlGGyeHfh2c+kIYUfGxdCMcp1wRHgEcWhbWvR2TTLiu JHkfjtnVlLZHRdb3lpJJtxUv+1DCpTbUl/dYs= Received: by 10.50.173.98 with SMTP id bj2mr2682936igc.27.1327067978063; Fri, 20 Jan 2012 05:59:38 -0800 (PST) Received: from [192.168.1.65] (99-121-78-10.lightspeed.lnngmi.sbcglobal.net. [99.121.78.10]) by mx.google.com with ESMTPS id r18sm10283283ibh.4.2012.01.20.05.59.34 (version=SSLv3 cipher=OTHER); Fri, 20 Jan 2012 05:59:37 -0800 (PST) Message-ID: <4F197343.8070704@gmail.com> Date: Fri, 20 Jan 2012 08:59:31 -0500 From: Edgar Friendly User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111124 Thunderbird/8.0 MIME-Version: 1.0 To: caml-list@inria.fr References: <20120120093834.GB1870@siouxsie> In-Reply-To: <20120120093834.GB1870@siouxsie> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [Caml-list] is there a more concise way to write this? On 01/20/2012 04:38 AM, oliver wrote: > More concise does not always mean better readable or more performant. > You apply the same kind of selection for both values. I can't measure readability, but I did throw together a quick benchmark to test the different methods. Please take no offense at this - I'm sure that the responses were headed much more towards readability than performance, so comparing them on performance is totally unfair, and this in no way judges the people whose names I've used to label the solutions. Maybe not surprisingly, the original match-based code came out fastest, and List.filter the slowest. Here's the result summary: demello (83.95 ns) is probably (alpha=7.88%) same speed as friendly (88.68 ns) which is probably (alpha=23.96%) same speed as vuillion (96.68 ns) which is 34.6% faster than robert (147.94 ns) which is probably (alpha=8.43%) same speed as ferre (154.63 ns) which is 70.7% faster than lin (527.64 ns) which is 17.1% faster than lefessant (636.43 ns) which is 31.1% faster than lin2 (924.24 ns) Source code and full results follow. E. --------------------------------- let o = 1 and v = 2 let demello = function | (true, true) -> [o; v] | (false, true) -> [v] | (true, false) -> [o] | (false, false) -> [] let robert (out, value) = (if out then [o] else []) @ (if value then [v] else []) let b2l b x = if b then [x] else [];; let ferre (out, value) = b2l out o @ b2l value v;; let maybe b v c = if b then v :: c else c let vuillion (out, value) = maybe out o (maybe value v []) let (|>) x f = f x (* no %revapply yet *) let lefessant (out, value) = [] |> maybe value v |> maybe out o let lin (out, value) = List.filter (fun x -> x) [out; value] let lin2 (out, value) = List.map snd (List.filter (fun x -> fst x) [(out, o); (value, v)]) let friendly (out,value) = if out then if value then [o;v] else [o] else if value then [v] else [] let test f n = for i = 1 to n do ignore(f (true, true)); ignore(f (true, false)); ignore(f (false, true)); ignore(f (false, false)); done let () = Bench.bench_n [ "demello", test demello; "robert", test robert; "ferre", test ferre; "vuillion", test vuillion; "lefessant", test lefessant; "lin", test lin; "lin2", test lin2; "friendly", test friendly; ] |> Bench.summarize ~alpha:0.05 ------------------------------ Measuring: System Clock Warming up Estimating clock resolution (1.44 us) Estimating cost of timer call (1.60 us) Benchmarking: demello Ran 32768 iterations in 1.68 ms Collecting 300 samples, 28127 iterations each, estimated time: 431.61 ms N: 300 Inter-quartile width:16.65 ns, Full range: (43.99 ns,525.38 ns) Outliers: 7 (2.3%) High Mild, 19 (6.3%) High Severe, mean: 83.95 ns, 95% CI: (75.16 ns, 88.32 ns) std.dev.: 48.91 ns, 95% CI: (27.18 ns, 61.20 ns) Benchmarking: robert Ran 16384 iterations in 2.96 ms Collecting 300 samples, 7956 iterations each, estimated time: 431.66 ms N: 300 Inter-quartile width:19.52 ns, Full range: (102.86 ns,644.99 ns) Outliers: 6 (2.0%) High Mild, 9 (3.0%) High Severe, mean: 147.94 ns, 95% CI: (141.11 ns, 153.17 ns) std.dev.: 53.76 ns, 95% CI: (22.29 ns, 68.36 ns) Benchmarking: ferre Ran 16384 iterations in 2.71 ms Collecting 300 samples, 8683 iterations each, estimated time: 431.64 ms N: 300 Inter-quartile width:25.01 ns, Full range: (104.02 ns,795.03 ns) Outliers: 6 (2.0%) High Mild, 11 (3.7%) High Severe, mean: 154.63 ns, 95% CI: (149.85 ns, 168.24 ns) std.dev.: 64.76 ns, 95% CI: (46.73 ns, 100.11 ns) Benchmarking: vuillion Ran 32768 iterations in 8.24 ms Collecting 300 samples, 5724 iterations each, estimated time: 431.66 ms N: 300 Inter-quartile width:26.91 ns, Full range: (38.71 ns,1.60 us) Outliers: 14 (4.7%) High Severe, mean: 96.68 ns, 95% CI: (72.67 ns, 115.55 ns) std.dev.: 193.29 ns, 95% CI: (99.28 ns, 244.72 ns) Benchmarking: lefessant Ran 4096 iterations in 2.32 ms Collecting 300 samples, 2543 iterations each, estimated time: 431.72 ms N: 300 Inter-quartile width:147.38 ns, Full range: (346.26 ns,4.15 us) Outliers: 3 (1.0%) High Severe, mean: 636.43 ns, 95% CI: (602.68 ns, 664.49 ns) std.dev.: 277.24 ns, 95% CI: (114.65 ns, 400.85 ns) Benchmarking: lin Ran 4096 iterations in 2.04 ms Collecting 300 samples, 2891 iterations each, estimated time: 431.73 ms N: 300 Inter-quartile width:190.01 ns, Full range: (305.90 ns,1.86 us) Outliers: 2 (0.7%) High Severe, mean: 527.64 ns, 95% CI: (514.62 ns, 556.02 ns) std.dev.: 152.73 ns, 95% CI: (117.57 ns, 225.48 ns) Benchmarking: lin2 Ran 4096 iterations in 2.49 ms Collecting 300 samples, 2371 iterations each, estimated time: 431.71 ms N: 300 Inter-quartile width:252.57 ns, Full range: (505.02 ns,5.90 us) Outliers: 10 (3.3%) High Severe, mean: 924.24 ns, 95% CI: (863.30 ns, 973.78 ns) std.dev.: 469.52 ns, 95% CI: (247.89 ns, 617.19 ns) Benchmarking: friendly Ran 16384 iterations in 1.49 ms Collecting 300 samples, 15769 iterations each, estimated time: 431.63 ms N: 300 Inter-quartile width:15.57 ns, Full range: (49.11 ns,480.53 ns) Outliers: 10 (3.3%) Low Mild, 6 (2.0%) High Severe, mean: 88.68 ns, 95% CI: (86.37 ns, 94.85 ns) std.dev.: 31.12 ns, 95% CI: (13.41 ns, 44.10 ns)