forked from ocaml-community/yojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.ml
53 lines (42 loc) · 1.13 KB
/
bench.ml
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
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