use the branch 'tree' of benchmark

This commit is contained in:
Simon Cruanes 2014-12-22 23:18:27 +01:00
parent bd84b620ec
commit 6964675481

View file

@ -1,9 +1,8 @@
(** Generic benchs *) (** Generic benchs *)
(* composition *) let (@>) = Benchmark.Tree.(@>)
let (%%) f g x = f (g x) let (@>>) = Benchmark.Tree.(@>>)
let (@>>>) = Benchmark.Tree.(@>>>)
(* FIXME: find out why -tree takes so long *)
module L = struct module L = struct
(* FLAT MAP *) (* FLAT MAP *)
@ -13,72 +12,66 @@ 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_flat_map ?(time=2) n = let bench_flat_map ?(time=2) n = ("flat_map_" ^ string_of_int n) @> lazy(
let l = lazy CCList.(1 -- n) in let l = CCList.(1 -- n) in
let flatten_map_ l = List.flatten (CCList.map f_ l) let flatten_map_ l = List.flatten (CCList.map f_ l)
and flatten_ccmap_ l = List.flatten (List.map f_ l) in and flatten_ccmap_ l = List.flatten (List.map f_ l) in
CCBench.throughputN time Benchmark.throughputN time
[ "flat_map", CCList.flat_map f_ %% Lazy.force, l [ "flat_map", CCList.flat_map f_, l
; "flatten o CCList.map", flatten_ccmap_ %% Lazy.force, l ; "flatten o CCList.map", flatten_ccmap_, l
; "flatten o map", flatten_map_ %% Lazy.force, l ; "flatten o map", flatten_map_, l
] ]
)
(* APPEND *) (* APPEND *)
let append_ f (lazy l1, lazy l2, lazy l3) = let append_ f (l1, l2, l3) =
ignore (f (f l1 l2) l3) ignore (f (f l1 l2) l3)
let bench_append ?(time=2) n = let bench_append ?(time=2) n = ("append_" ^ string_of_int n) @> lazy (
let l1 = lazy CCList.(1 -- n) in let l1 = CCList.(1 -- n) in
let l2 = lazy CCList.(n+1 -- 2*n) in let l2 = CCList.(n+1 -- 2*n) in
let l3 = lazy 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
CCBench.throughputN time Benchmark.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
] ]
)
(* FLATTEN *) (* FLATTEN *)
let bench_flatten ?(time=2) n = let bench_flatten ?(time=2) n = ("flatten_" ^ string_of_int n) @> lazy (
let fold_right_append_ l = let fold_right_append_ l =
List.fold_right List.append l [] List.fold_right List.append l []
and cc_fold_right_append_ l = and cc_fold_right_append_ l =
CCList.fold_right CCList.append l [] CCList.fold_right CCList.append l []
in in
let l = lazy ( let l =
CCList.Idx.mapi CCList.Idx.mapi
(fun i x -> CCList.(x -- (x+ min i 100))) (fun i x -> CCList.(x -- (x+ min i 100)))
CCList.(1 -- n)) CCList.(1 -- n)
in in
CCBench.throughputN time Benchmark.throughputN time
[ "CCList.flatten", CCList.flatten %% Lazy.force, l [ "CCList.flatten", CCList.flatten, l
; "List.flatten", List.flatten %% Lazy.force, l ; "List.flatten", List.flatten, l
; "fold_right append", fold_right_append_ %% Lazy.force, l ; "fold_right append", fold_right_append_, l
; "CCList.(fold_right append)", cc_fold_right_append_ %% Lazy.force, l ; "CCList.(fold_right append)", cc_fold_right_append_, l
] ]
)
(* MAIN *) (* MAIN *)
let () = CCBench.register CCBench.( let () = Benchmark.Tree.(register (
"list" >::: "list" @>>>
[ "flat_map" >:: [ "flat_map" @>>
map_int with_int (bench_flat_map ~time:2) [100; 10_000; 100_00]
[ bench_flat_map ~time:2, 100 ; "flatten" @>>
; bench_flat_map ~time:2, 10_000 with_int (bench_flatten ~time:2) [100; 10_000; 100_000]
; bench_flat_map ~time:4, 100_000] ; "append" @>>
; "flatten" >:: with_int (bench_append ~time:2) [100; 10_000; 100_000]
map_int
[ 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]
] ]
) ))
end end
module Vec = struct module Vec = struct
@ -94,36 +87,38 @@ module Vec = struct
CCVector.iter (fun x -> CCVector.push v' (f x)) v; CCVector.iter (fun x -> CCVector.push v' (f x)) v;
v' v'
let bench_map n = let bench_map n = "map" @> lazy (
let v = lazy (CCVector.init n (fun x->x)) in let v = CCVector.init n (fun x->x) in
CCBench.throughputN 2 Benchmark.throughputN 2
[ "map", CCVector.map f %% Lazy.force, v [ "map", CCVector.map f, v
; "map_push", map_push_ f %% Lazy.force, v ; "map_push", map_push_ f, v
; "map_push_cap", map_push_size_ f %% Lazy.force, v ; "map_push_cap", map_push_size_ f, v
] ]
)
let try_append_ app n v2 () = let try_append_ app n v2 () =
let v1 = CCVector.init n (fun x->x) in let v1 = CCVector.init n (fun x->x) in
app v1 (Lazy.force v2); app v1 v2;
assert (CCVector.length v1 = 2*n); assert (CCVector.length v1 = 2*n);
() ()
let append_naive_ v1 v2 = let append_naive_ v1 v2 =
CCVector.iter (fun x -> CCVector.push v1 x) v2 CCVector.iter (fun x -> CCVector.push v1 x) v2
let bench_append n = let bench_append n = "append" @> lazy (
let v2 = lazy (CCVector.init n (fun x->n+x)) in let v2 = CCVector.init n (fun x->n+x) in
CCBench.throughputN 2 Benchmark.throughputN 2
[ "append", try_append_ CCVector.append n v2, () [ "append", try_append_ CCVector.append n v2, ()
; "append_naive", try_append_ append_naive_ n v2, () ; "append_naive", try_append_ append_naive_ n v2, ()
] ]
let () = CCBench.register CCBench.(
"vector" >:::
[ "map" >:: with_int bench_map [100; 10_000; 100_000]
; "append" >:: with_int bench_append [100; 10_000; 50_000]
]
) )
let () = Benchmark.Tree.(register (
"vector" @>>>
[ with_int bench_map [100; 10_000; 100_000]
; with_int bench_append [100; 10_000; 50_000]
]
))
end end
module Cache = struct module Cache = struct
@ -142,7 +137,7 @@ module Cache = struct
C.clear c; C.clear c;
f x f x
let bench_fib n = let bench_fib n = "fib" @> lazy (
let l = let l =
[ "replacing_fib (128)", make_fib (C.replacing 128), n [ "replacing_fib (128)", make_fib (C.replacing 128), n
; "LRU_fib (128)", make_fib (C.lru 128), n ; "LRU_fib (128)", make_fib (C.lru 128), n
@ -158,13 +153,14 @@ module Cache = struct
] @ l ] @ l
else l else l
in in
CCBench.throughputN 3 l Benchmark.throughputN 3 l
let () = CCBench.register CCBench.(
"cache" >:::
[ "fib" >:: with_int bench_fib [10; 20; 100; 200; 1_000;]
]
) )
let () = Benchmark.Tree.(register (
"cache" @>>>
[ with_int bench_fib [10; 20; 100; 200; 1_000;]
]
))
end end
module Tbl = struct module Tbl = struct
@ -266,8 +262,8 @@ module Tbl = struct
done; done;
h h
let bench_maps1 n = let bench_maps1 n = "add" @> lazy (
CCBench.throughputN 3 Benchmark.throughputN 3
["phashtbl_add", (fun n -> ignore (phashtbl_add n)), n; ["phashtbl_add", (fun n -> ignore (phashtbl_add n)), n;
"hashtbl_add", (fun n -> ignore (hashtbl_add n)), n; "hashtbl_add", (fun n -> ignore (hashtbl_add n)), n;
"ihashtbl_add", (fun n -> ignore (ihashtbl_add n)), n; "ihashtbl_add", (fun n -> ignore (ihashtbl_add n)), n;
@ -278,6 +274,7 @@ module Tbl = struct
"imap_add", (fun n -> ignore (imap_add n)), n; "imap_add", (fun n -> ignore (imap_add n)), n;
"ccflathashtbl_add", (fun n -> ignore (icchashtbl_add n)), n; "ccflathashtbl_add", (fun n -> ignore (icchashtbl_add n)), n;
] ]
)
let phashtbl_replace n = let phashtbl_replace n =
let h = PHashtbl.create 50 in let h = PHashtbl.create 50 in
@ -369,8 +366,8 @@ module Tbl = struct
done; done;
h h
let bench_maps2 n = let bench_maps2 n = "replace" @> lazy (
CCBench.throughputN 3 Benchmark.throughputN 3
["phashtbl_replace", (fun n -> ignore (phashtbl_replace n)), n; ["phashtbl_replace", (fun n -> ignore (phashtbl_replace n)), n;
"hashtbl_replace", (fun n -> ignore (hashtbl_replace n)), n; "hashtbl_replace", (fun n -> ignore (hashtbl_replace n)), n;
"ihashtbl_replace", (fun n -> ignore (ihashtbl_replace n)), n; "ihashtbl_replace", (fun n -> ignore (ihashtbl_replace n)), n;
@ -380,7 +377,7 @@ module Tbl = struct
"skiplist_replace", (fun n -> ignore (skiplist_replace n)), n; "skiplist_replace", (fun n -> ignore (skiplist_replace n)), n;
"imap_replace", (fun n -> ignore (imap_replace n)), n; "imap_replace", (fun n -> ignore (imap_replace n)), n;
"ccflathashtbl_replace", (fun n -> ignore (icchashtbl_replace n)), n; "ccflathashtbl_replace", (fun n -> ignore (icchashtbl_replace n)), n;
] ])
let my_len = 250 let my_len = 250
@ -444,7 +441,7 @@ module Tbl = struct
ignore (ICCHashtbl.get_exn i m); ignore (ICCHashtbl.get_exn i m);
done done
let bench_maps3 n = let bench_maps3 n = "find" @> lazy (
let h = phashtbl_add n in let h = phashtbl_add n in
let h' = hashtbl_add n in let h' = hashtbl_add n in
let h'' = ihashtbl_add n in let h'' = ihashtbl_add n in
@ -455,7 +452,7 @@ module Tbl = struct
let a = Array.init n (fun i -> string_of_int i) in let a = Array.init n (fun i -> string_of_int i) in
let m = imap_add n in let m = imap_add n in
let h'''''' = icchashtbl_add n in let h'''''' = icchashtbl_add n in
CCBench.throughputN 3 [ Benchmark.throughputN 3 [
"phashtbl_find", (fun () -> phashtbl_find h n), (); "phashtbl_find", (fun () -> phashtbl_find h n), ();
"hashtbl_find", (fun () -> hashtbl_find h' n), (); "hashtbl_find", (fun () -> hashtbl_find h' n), ();
"ihashtbl_find", (fun () -> ihashtbl_find h'' n), (); "ihashtbl_find", (fun () -> ihashtbl_find h'' n), ();
@ -466,30 +463,32 @@ module Tbl = struct
"array_find", (fun () -> array_find a n), (); "array_find", (fun () -> array_find a n), ();
"imap_find", (fun () -> imap_find m n), (); "imap_find", (fun () -> imap_find m n), ();
"cchashtbl_find", (fun () -> icchashtbl_find h'''''' n), (); "cchashtbl_find", (fun () -> icchashtbl_find h'''''' n), ();
]
let () = CCBench.register CCBench.(
"tbl" >:::
[ "add" >:: with_int bench_maps1 [10; 100; 1_000; 10_000;]
; "replace" >:: with_int bench_maps2 [10; 100; 1_000; 10_000]
; "find" >:: with_int bench_maps3 [10; 20; 100; 1_000; 10_000]
]) ])
let () = Benchmark.Tree.(register (
"tbl" @>>>
[ with_int bench_maps1 [10; 100; 1_000; 10_000;]
; with_int bench_maps2 [10; 100; 1_000; 10_000]
; with_int bench_maps3 [10; 20; 100; 1_000; 10_000]
]
))
end end
module Iter = struct module Iter = struct
(** {2 Sequence/Gen} *) (** {2 Sequence/Gen} *)
let bench_fold n = let bench_fold n = "fold" @> lazy (
let seq () = Sequence.fold (+) 0 Sequence.(0 --n) in let seq () = Sequence.fold (+) 0 Sequence.(0 --n) in
let gen () = Gen.fold (+) 0 Gen.(0 -- n) in let gen () = Gen.fold (+) 0 Gen.(0 -- n) in
let klist () = CCKList.fold (+) 0 CCKList.(0 -- n) in let klist () = CCKList.fold (+) 0 CCKList.(0 -- n) in
CCBench.throughputN 3 Benchmark.throughputN 3
[ "sequence.fold", seq, (); [ "sequence.fold", seq, ();
"gen.fold", gen, (); "gen.fold", gen, ();
"klist.fold", klist, (); "klist.fold", klist, ();
] ]
)
let bench_flat_map n = let bench_flat_map n = "flat_map" @> lazy (
let seq () = Sequence.( let seq () = Sequence.(
0 -- n |> flat_map (fun x -> x-- (x+10)) |> fold (+) 0 0 -- n |> flat_map (fun x -> x-- (x+10)) |> fold (+) 0
) )
@ -500,13 +499,14 @@ module Iter = struct
0 -- n |> flat_map (fun x -> x-- (x+10)) |> fold (+) 0 0 -- n |> flat_map (fun x -> x-- (x+10)) |> fold (+) 0
) )
in in
CCBench.throughputN 3 Benchmark.throughputN 3
[ "sequence.flat_map", seq, (); [ "sequence.flat_map", seq, ();
"gen.flat_map", gen, (); "gen.flat_map", gen, ();
"klist.flat_map", klist, (); "klist.flat_map", klist, ();
] ]
)
let bench_iter n = let bench_iter n = "iter" @> lazy (
let seq () = let seq () =
let i = ref 2 in let i = ref 2 in
Sequence.( Sequence.(
@ -523,18 +523,20 @@ module Iter = struct
1 -- n |> iter (fun x -> i := !i * x) 1 -- n |> iter (fun x -> i := !i * x)
) )
in in
CCBench.throughputN 3 Benchmark.throughputN 3
[ "sequence.iter", seq, (); [ "sequence.iter", seq, ();
"gen.iter", gen, (); "gen.iter", gen, ();
"klist.iter", klist, (); "klist.iter", klist, ();
] ]
)
let () = CCBench.register CCBench.( let () = Benchmark.Tree.(register (
"iter" >::: "iter" @>>>
[ "fold" >:: with_int bench_fold [100; 1_000; 10_000; 1_000_000] [ with_int bench_fold [100; 1_000; 10_000; 1_000_000]
; "flat_map" >:: with_int bench_flat_map [1_000; 10_000] ; with_int bench_flat_map [1_000; 10_000]
; "iter" >:: with_int bench_iter [1_000; 10_000] ; with_int bench_iter [1_000; 10_000]
]) ]
))
end end
module Batch = struct module Batch = struct
@ -578,7 +580,7 @@ module Batch = struct
ignore (collect a); ignore (collect a);
a a
let bench_for ~time n = let bench_for ~time n = "batch" @> lazy (
let a = C.(0 -- n) in let a = C.(0 -- n) in
(* debug (* debug
CCPrint.printf "naive: %a\n" (CCArray.pp CCInt.pp) (naive a); CCPrint.printf "naive: %a\n" (CCArray.pp CCInt.pp) (naive a);
@ -586,16 +588,17 @@ module Batch = struct
CCPrint.printf "batch: %a\n" (CCArray.pp CCInt.pp) (batch a); CCPrint.printf "batch: %a\n" (CCArray.pp CCInt.pp) (batch a);
*) *)
assert (C.equal (batch a) (naive a)); assert (C.equal (batch a) (naive a));
CCBench.throughputN time Benchmark.throughputN time
[ C.name ^ "_naive", naive, a [ C.name ^ "_naive", naive, a
; C.name ^ "_batch", batch, a ; C.name ^ "_batch", batch, a
] ]
)
let bench = CCBench.( let bench = Benchmark.(
C.name >:: map_int C.name @>>>
[ bench_for ~time:1, 100 [ bench_for ~time:1 100
; bench_for ~time:4, 100_000 ; bench_for ~time:4 100_000
; bench_for ~time:4, 1_000_000 ; bench_for ~time:4 1_000_000
]) ])
end end
@ -622,13 +625,14 @@ module Batch = struct
let doubleton x y = CCKList.of_list [ x; y ] let doubleton x y = CCKList.of_list [ x; y ]
end) end)
let () = CCBench.register CCBench.( let () = Benchmark.Tree.(register (
"batch" >:: mk_list "batch" @>>>
[ BenchKList.bench [ BenchKList.bench
; BenchArray.bench ; BenchArray.bench
; BenchList.bench ; BenchList.bench
]) ]
))
end end
let () = let () =
CCBench.run_main () Benchmark.Tree.run_global ()