File: tuple_match.ml

package info (click to toggle)
js-of-ocaml 6.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,932 kB
  • sloc: ml: 135,957; javascript: 58,364; ansic: 437; makefile: 422; sh: 12; perl: 4
file content (56 lines) | stat: -rw-r--r-- 1,224 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(* TEST *)

let[@inline never] small_match n x =
  let (left, right) = match x with
    | 0 -> n, 42
    | 1 -> 42, n
    | _ -> assert false in
  left - right

let[@inline never] big_match n x =
  let (left, right) = match x with
    | 0 -> n, 42
    | 1 -> 42, n
    | 2 -> 42-n, 0
    | 3 -> 0, 42-n
    | 4 -> n/2, n/2
    | 5 -> n, n
    | _ -> assert false in
  left - right

let[@inline never] string_match n x =
  let (left, right) = match x with
    | "0" -> n, 42
    | "1" -> 42, n
    | "2" -> 42-n, 0
    | "3" -> 0, 42-n
    | "4" -> n/2, n/2
    | "5" -> n, n
    | _ -> assert false in
  left - right




let printf = Printf.printf

let test f n i =
  let mw_overhead =
    let a = Gc.minor_words () in
    let b = Gc.minor_words () in
    b -. a in
  let mw = Gc.minor_words () in
  let k = f n i in
  assert (k = 0);
  let mw' = Gc.minor_words () in
  let delta = int_of_float (mw' -. mw -. mw_overhead) in
  printf "allocated %d words\n" delta

let () =
  let n = 42 in
  printf "small_match:\n";
  for i = 0 to 1 do test small_match n i done;
  printf "big_match:\n";
  for i = 0 to 5 do test big_match n i done;
  printf "string_match:\n";
  for i = 0 to 5 do test string_match n (string_of_int i) done