Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Edgar Friendly <thelema314@gmail.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] is there a more concise way to write this?
Date: Fri, 20 Jan 2012 08:59:31 -0500	[thread overview]
Message-ID: <4F197343.8070704@gmail.com> (raw)
In-Reply-To: <20120120093834.GB1870@siouxsie>

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)

  reply	other threads:[~2012-01-20 13:59 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-20  6:38 Martin DeMello
2012-01-20  6:46 ` Valentin ROBERT
2012-01-20  6:58   ` Martin DeMello
2012-01-20  8:37   ` David Allsopp
2012-01-20 13:29     ` Edgar Friendly
2012-01-20 13:50       ` Fabrice Le Fessant
2012-01-20 13:58       ` oliver
2012-01-20 14:05         ` Edgar Friendly
2012-01-20 14:12       ` David Allsopp
2012-01-20 14:23         ` Fabrice Le Fessant
2012-01-20 14:23         ` Edgar Friendly
2012-01-20  8:37   ` Sebastien Ferre
2012-01-20  9:11     ` Jerome Vouillon
2012-01-20  9:34       ` Fabrice Le Fessant
2012-01-20 10:27         ` Arnaud Spiwack
2012-01-20  8:52 ` Lin
2012-01-20  9:08   ` Valentin ROBERT
2012-01-20  9:19     ` Lin
2012-01-20 10:21     ` Martin DeMello
2012-01-20  9:38 ` oliver
2012-01-20 13:59   ` Edgar Friendly [this message]
2012-01-20 14:42     ` oliver
2012-01-20 15:31     ` Fabrice Le Fessant
2012-01-20 21:04     ` oliver
2012-01-20 21:09       ` oliver
2012-01-20 20:40 ` oliver
2012-01-20 21:07   ` Martin DeMello

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=4F197343.8070704@gmail.com \
    --to=thelema314@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