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)

--
Caml-list mailing list.  Subscription management and archives:
https://sympa-roc.inria.fr/wws/info/caml-list
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs

Reply via email to