mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
benchmark Conv.from (from json)
This commit is contained in:
parent
3c4270b3a1
commit
a2e0d85dca
1 changed files with 38 additions and 0 deletions
|
|
@ -40,6 +40,39 @@ let bench_point_list x =
|
||||||
] in
|
] in
|
||||||
Benchmark.tabulate res
|
Benchmark.tabulate res
|
||||||
|
|
||||||
|
(* conversion back from json *)
|
||||||
|
let rec point_of_json_manual (j:Conv.Json.t) =
|
||||||
|
let module P = Point in
|
||||||
|
match j with
|
||||||
|
| `Assoc l ->
|
||||||
|
let x = List.assoc "x" l in
|
||||||
|
let y = List.assoc "y" l in
|
||||||
|
let color = List.assoc "color" l in
|
||||||
|
let prev = List.assoc "prev" l in
|
||||||
|
let prev = match prev with
|
||||||
|
| `String "none" -> None
|
||||||
|
| `List [`String "some"; p'] -> Some (point_of_json_manual p')
|
||||||
|
| _ -> failwith "expected point"
|
||||||
|
in
|
||||||
|
begin match x, y, color with
|
||||||
|
| `Int x, `Int y, `String color -> P.({x;y;color;prev;})
|
||||||
|
| _ -> failwith "expected point"
|
||||||
|
end
|
||||||
|
| _ -> failwith "expected point"
|
||||||
|
|
||||||
|
let points_of_json_manual = function
|
||||||
|
| `List l -> List.map point_of_json_manual l
|
||||||
|
| _ -> failwith "expected list of points"
|
||||||
|
|
||||||
|
let points_of_json_conv =
|
||||||
|
Conv.from Conv.Json.source (Conv.Sink.list_ Point.sink)
|
||||||
|
|
||||||
|
let bench_point_list_back l =
|
||||||
|
let res = Benchmark.throughputN 5
|
||||||
|
[ "conv", points_of_json_conv, l
|
||||||
|
; "manual", points_of_json_manual, l
|
||||||
|
] in
|
||||||
|
Benchmark.tabulate res
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
Printf.printf "list of 5 elements...\n";
|
Printf.printf "list of 5 elements...\n";
|
||||||
|
|
@ -52,4 +85,9 @@ let () =
|
||||||
let l = Gen.(repeat Point.p |> take 10 |> to_rev_list) in
|
let l = Gen.(repeat Point.p |> take 10 |> to_rev_list) in
|
||||||
Printf.printf "list of %d points...\n" (List.length l);
|
Printf.printf "list of %d points...\n" (List.length l);
|
||||||
bench_point_list l;
|
bench_point_list l;
|
||||||
|
|
||||||
|
(* convert back from json *)
|
||||||
|
let l' = conv_list_point_to_json l in
|
||||||
|
Printf.printf "from JSON...\n";
|
||||||
|
bench_point_list_back l';
|
||||||
()
|
()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue