File: bench.ml

package info (click to toggle)
yojson 1.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 252 kB
  • sloc: ml: 1,957; makefile: 208
file content (55 lines) | stat: -rw-r--r-- 1,174 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
(* $Id$ *)

open Printf

let data =
  let l = ref [] in
  try
    while true do
      l := input_line stdin :: !l
    done;
    assert false
  with End_of_file -> String.concat "\n" (List.rev !l)

let yojson_data = Yojson.Safe.from_string data
let jsonwheel_data = Json_io.json_of_string data

let n = 10_000

let yojson_rd_loop () =
  for i = 1 to n do
    ignore (Yojson.Safe.from_string data)
  done

let yojson_wr_loop () =
  for i = 1 to n do
    ignore (Yojson.Safe.to_string yojson_data)
  done

let jsonwheel_rd_loop () =
  for i = 1 to n do
    ignore (Json_io.json_of_string data)
  done

let jsonwheel_wr_loop () =
  for i = 1 to n do
    ignore (Json_io.string_of_json ~compact:true jsonwheel_data)
  done

let time msg f =
  let t1 = Unix.gettimeofday () in
  f ();
  let t2 = Unix.gettimeofday () in
  printf "%s: %.3f\n%!" msg (t2 -. t1)

let () =
  time "rd yojson" yojson_rd_loop;
  time "rd json-wheel" jsonwheel_rd_loop;
  time "rd yojson" yojson_rd_loop;
  time "rd json-wheel" jsonwheel_rd_loop;

  time "wr yojson" yojson_wr_loop;
  time "wr json-wheel" jsonwheel_wr_loop;
  time "wr yojson" yojson_wr_loop;
  time "wr json-wheel" jsonwheel_wr_loop