mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
use CCBench in benchs/benchs.ml to hierarchize benchmarks
This commit is contained in:
parent
2606833518
commit
55e18bbb0f
2 changed files with 31 additions and 60 deletions
2
.merlin
2
.merlin
|
|
@ -4,12 +4,14 @@ S string
|
||||||
S pervasives
|
S pervasives
|
||||||
S tests
|
S tests
|
||||||
S examples
|
S examples
|
||||||
|
S benchs
|
||||||
B _build/core
|
B _build/core
|
||||||
B _build/misc
|
B _build/misc
|
||||||
B _build/string
|
B _build/string
|
||||||
B _build/pervasives
|
B _build/pervasives
|
||||||
B _build/tests
|
B _build/tests
|
||||||
B _build/examples
|
B _build/examples
|
||||||
|
B _build/benchs/
|
||||||
PKG oUnit
|
PKG oUnit
|
||||||
PKG benchmark
|
PKG benchmark
|
||||||
PKG threads
|
PKG threads
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,5 @@
|
||||||
(** Generic benchs *)
|
(** Generic benchs *)
|
||||||
|
|
||||||
let draw_line () =
|
|
||||||
output_string stdout (CCString.repeat "*" 80);
|
|
||||||
output_char stdout '\n'
|
|
||||||
|
|
||||||
module L = struct
|
module L = struct
|
||||||
|
|
||||||
(* FLAT MAP *)
|
(* FLAT MAP *)
|
||||||
|
|
@ -13,16 +9,13 @@ module L = struct
|
||||||
else if x mod 5 = 1 then [x;x+1]
|
else if x mod 5 = 1 then [x;x+1]
|
||||||
else [x;x+1;x+2;x+3]
|
else [x;x+1;x+2;x+3]
|
||||||
|
|
||||||
let bench_flatmap ?(time=2) n =
|
let bench_flat_map ?(time=2) n =
|
||||||
draw_line ();
|
|
||||||
Printf.printf "flat_map for %d elements\n" n;
|
|
||||||
let l = CCList.(1 -- n) in
|
let l = CCList.(1 -- n) in
|
||||||
let res = Benchmark.throughputN time
|
CCBench.throughputN time
|
||||||
[ "flat_map", CCList.flat_map f_, l
|
[ "flat_map", CCList.flat_map f_, l
|
||||||
; "flatten o CCList.map", (fun l -> List.flatten (CCList.map f_ l)), l
|
; "flatten o CCList.map", (fun l -> List.flatten (CCList.map f_ l)), l
|
||||||
; "flatten o map", (fun l -> List.flatten (List.map f_ l)), l
|
; "flatten o map", (fun l -> List.flatten (List.map f_ l)), l
|
||||||
] in
|
]
|
||||||
Benchmark.tabulate res
|
|
||||||
|
|
||||||
(* APPEND *)
|
(* APPEND *)
|
||||||
|
|
||||||
|
|
@ -30,76 +23,52 @@ module L = struct
|
||||||
ignore (f (f l1 l2) l3)
|
ignore (f (f l1 l2) l3)
|
||||||
|
|
||||||
let bench_append ?(time=2) n =
|
let bench_append ?(time=2) n =
|
||||||
draw_line ();
|
|
||||||
Printf.printf "append for %d elements\n" n;
|
|
||||||
let l1 = CCList.(1 -- n) in
|
let l1 = CCList.(1 -- n) in
|
||||||
let l2 = CCList.(n+1 -- 2*n) in
|
let l2 = CCList.(n+1 -- 2*n) in
|
||||||
let l3 = CCList.(2*n+1 -- 3*n) in
|
let l3 = CCList.(2*n+1 -- 3*n) in
|
||||||
let arg = l1, l2, l3 in
|
let arg = l1, l2, l3 in
|
||||||
let res = Benchmark.throughputN time
|
CCBench.throughputN time
|
||||||
[ "CCList.append", append_ CCList.append, arg
|
[ "CCList.append", append_ CCList.append, arg
|
||||||
; "List.append", append_ List.append, arg
|
; "List.append", append_ List.append, arg
|
||||||
] in
|
]
|
||||||
Benchmark.tabulate res
|
|
||||||
|
|
||||||
(* FLATTEN *)
|
(* FLATTEN *)
|
||||||
|
|
||||||
let bench_flatten ?(time=2) n =
|
let bench_flatten ?(time=2) n =
|
||||||
draw_line ();
|
|
||||||
Printf.printf "flatten for %d elements\n" n;
|
|
||||||
let l = CCList.Idx.mapi (fun i x -> CCList.(x -- (x+ min i 100))) CCList.(1 -- n) in
|
let l = CCList.Idx.mapi (fun i x -> CCList.(x -- (x+ min i 100))) CCList.(1 -- n) in
|
||||||
let res = Benchmark.throughputN time
|
CCBench.throughputN time
|
||||||
[ "CCList.flatten", CCList.flatten, l
|
[ "CCList.flatten", CCList.flatten, l
|
||||||
; "List.flatten", List.flatten, l
|
; "List.flatten", List.flatten, l
|
||||||
; "fold_right append", (fun l -> List.fold_right List.append l []), l
|
; "fold_right append", (fun l -> List.fold_right List.append l []), l
|
||||||
; "CCList.(fold_right append)", (fun l->CCList.fold_right CCList.append l []), l
|
; "CCList.(fold_right append)", (fun l->CCList.fold_right CCList.append l []), l
|
||||||
] in
|
]
|
||||||
Benchmark.tabulate res
|
|
||||||
|
|
||||||
|
|
||||||
(* MAIN *)
|
(* MAIN *)
|
||||||
|
|
||||||
let run() =
|
let bench = CCBench.(
|
||||||
bench_flatten 100;
|
"list" >:::
|
||||||
bench_flatten 10_000;
|
[ "flat_map" >::
|
||||||
bench_flatten ~time:4 100_000;
|
map_int
|
||||||
bench_flatmap 100;
|
[ bench_flat_map ~time:2, 100
|
||||||
bench_flatmap 10_000;
|
; bench_flat_map ~time:2, 10_000
|
||||||
bench_flatmap ~time:4 100_000;
|
; bench_flat_map ~time:4, 100_000]
|
||||||
bench_append 100;
|
; "flatten" >::
|
||||||
bench_append 10_000;
|
map_int
|
||||||
bench_append ~time:4 100_000;
|
[ bench_flatten ~time:2, 100
|
||||||
()
|
; bench_flatten ~time:2, 10_000
|
||||||
|
; bench_flatten ~time:4, 100_000]
|
||||||
|
; "append" >::
|
||||||
|
map_int
|
||||||
|
[ bench_append ~time:2, 100
|
||||||
|
; bench_append ~time:2, 10_000
|
||||||
|
; bench_append ~time:4, 100_000]
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
let () = CCBench.Glob.register bench
|
||||||
end
|
end
|
||||||
|
|
||||||
(* TODO *)
|
|
||||||
|
|
||||||
let tbl_ =
|
|
||||||
[ "list", L.run
|
|
||||||
]
|
|
||||||
|
|
||||||
let bench_all () =
|
|
||||||
List.iter (fun (name, run) ->
|
|
||||||
draw_line ();
|
|
||||||
Printf.printf "run tests for %s...\n" name;
|
|
||||||
run()
|
|
||||||
) tbl_
|
|
||||||
|
|
||||||
let which_ = ref ("all", bench_all)
|
|
||||||
let set_which s =
|
|
||||||
if s = "all" then which_ := s, bench_all
|
|
||||||
else try
|
|
||||||
let run = List.assoc s tbl_ in
|
|
||||||
which_ := s, run
|
|
||||||
with Not_found ->
|
|
||||||
failwith ("unknown test " ^ s)
|
|
||||||
let options = []
|
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
Arg.parse options set_which "benchs [which]";
|
CCBench.Glob.run_main ()
|
||||||
let name, run = !which_ in
|
|
||||||
Printf.printf "run test %s\n" name;
|
|
||||||
run ();
|
|
||||||
()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue