File: bench.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 (83 lines) | stat: -rw-r--r-- 1,700 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
open Bin_prot.Std

type element =
  { a : string
  ; b : string
  ; c : string
  ; d : bool
  ; e : bool
  ; f : bool
  ; g : string option
  ; h : bool
  ; i : bool
  ; j : bool
  ; k : bool
  ; l : bool
  ; m : bool
  ; n : bool
  ; o : string option
  ; p : bool
  ; q : bool
  ; r : int
  ; s : int
  ; t : int
  ; u : int
  ; v : string list (* these are small - 1-5 *)
  }
[@@deriving bin_io]

let s = "abcdefabcdefabcdef"

let v = [ s; s; s; s ]

let x =
  { a = s
  ; b = s
  ; c = s
  ; d = true
  ; e = true
  ; f = true
  ; g = Some s
  ; h = true
  ; i = true
  ; j = true
  ; k = true
  ; l = true
  ; m = true
  ; n = true
  ; o = Some s
  ; p = true
  ; q = true
  ; r = 65537
  ; s = 65537
  ; t = 65537
  ; u = 65537
  ; v
  }

type t = element list [@@deriving bin_io]

let rec f acc n = if n = 0 then acc else f (x :: acc) (n - 1)

let () =
  if Array.length Sys.argv > 1
  then (
    let count = int_of_string Sys.argv.(1) in
    let l = f [] count in
    let len = [%bin_size: t] l in
    let b = Bin_prot.Common.create_buf len in
    ignore ([%bin_write: t] b ~pos:0 l : int);
    let s = Bytes.create len in
    Bin_prot.Common.blit_buf_string ~src_pos:0 b ~dst_pos:0 s ~len;
    Out_channel.with_open_bin "data" @@ fun ch -> Out_channel.output_bytes ch s)
  else
    let s = In_channel.with_open_bin "data" @@ In_channel.input_all in
    let len = String.length s in
    let b = Bin_prot.Common.create_buf len in
    Bin_prot.Common.blit_string_buf ~src_pos:0 s ~dst_pos:0 b ~len;
    let t = Unix.gettimeofday () in
    for _ = 0 to 4 do
      ignore ([%bin_read: t] b ~pos_ref:(ref 0) : t)
    done;
    let t' = Unix.gettimeofday () in
    Format.printf "%.2f@." (t' -. t)