Extend benchmark: Sek, iter and pop

This commit is contained in:
Fabian 2020-04-19 17:58:39 +02:00
parent a1233392f0
commit d681a34caa

View file

@ -18,6 +18,24 @@ let repeat = 3
let (%%) f g x = f (g x)
module L = struct
let bench_iter ?(time=2) n =
let f i = ignore (Sys.opaque_identity i) in
let l = CCList.(1 -- n) in
let ral = CCRAL.of_list l in
let vec = CCFun_vec.of_list l in
let sek = Sek.Persistent.of_array 0 (Array.of_list l) in
let iter_list () = List.iter f l
and raliter () = CCRAL.iter ~f ral
and funvec_iter () = CCFun_vec.iter ~f vec
and sek_iter () = Sek.Persistent.iter Sek.forward f sek
in
B.throughputN time ~repeat
[ "List.iter", iter_list, ()
; "CCRAL.iter", raliter, ()
; "CCFun_vec.iter", funvec_iter, ()
; "Sek.Persistent.iter", sek_iter, ()
]
(* MAP *)
let f_ x = x+1
@ -81,10 +99,21 @@ module L = struct
let l1 = CCList.(1 -- n) in
let l2 = CCList.(n+1 -- 2*n) in
let l3 = CCList.(2*n+1 -- 3*n) in
let arg = l1, l2, l3 in
let v1 = CCFun_vec.of_list l1 in
let v2 = CCFun_vec.of_list l2 in
let v3 = CCFun_vec.of_list l3 in
let s1 = Sek.Persistent.of_array 0 (Array.of_list l1) in
let s2 = Sek.Persistent.of_array 0 (Array.of_list l2) in
let s3 = Sek.Persistent.of_array 0 (Array.of_list l3) in
let bench_list l1 l2 l3 () = ignore (Sys.opaque_identity (List.(append (append l1 l2) l3))) in
let bench_cclist l1 l2 l3 () = ignore (Sys.opaque_identity (CCList.(append (append l1 l2) l3))) in
let bench_funvec l1 l2 l3 () = ignore (Sys.opaque_identity (CCFun_vec.(append (append l1 l2) l3))) in
let bench_sek l1 l2 l3 () = ignore (Sys.opaque_identity (Sek.Persistent.(concat (concat l1 l2) l3))) in
B.throughputN time ~repeat
[ "CCList.append", append_ CCList.append, arg
; "List.append", append_ List.append, arg
[ "CCList.append", bench_list l1 l2 l3, ()
; "List.append", bench_cclist l1 l2 l3, ()
; "CCFun_vec.append", bench_funvec v1 v2 v3, ()
; "Sek.concat", bench_sek s1 s2 s3, ()
]
(* FLATTEN *)
@ -115,6 +144,7 @@ module L = struct
let v = CCFun_vec.of_list l in
let bv = BatVect.of_list l in
let map = List.fold_left (fun map i -> Int_map.add i i map) Int_map.empty l in
let sek = Sek.Persistent.of_array 0 (Array.of_list l) in
let bench_list l () =
for i = 0 to n-1 do Sys.opaque_identity (ignore (List.nth l i)) done
and bench_map l () =
@ -125,6 +155,8 @@ module L = struct
for i = 0 to n-1 do Sys.opaque_identity (ignore (CCFun_vec.get_exn i l)) done
and bench_batvec l () =
for i = 0 to n-1 do Sys.opaque_identity (ignore (BatVect.get l i)) done
and bench_sek l () =
for i = 0 to n-1 do Sys.opaque_identity (ignore (Sek.Persistent.get l i)) done
in
B.throughputN time ~repeat
[ "List.nth", bench_list l, ()
@ -132,6 +164,7 @@ module L = struct
; "RAL.get", bench_ral ral, ()
; "funvec.get", bench_funvec v, ()
; "batvec.get", bench_batvec bv, ()
; "Sek.Persistent.get", bench_sek sek, ()
]
let bench_set ?(time=2) n =
@ -139,6 +172,7 @@ module L = struct
let ral = CCRAL.of_list l in
(* let v = CCFun_vec.of_list l in *)
let bv = BatVect.of_list l in
let sek = Sek.Persistent.of_array 0 (Array.of_list l) in
let map = List.fold_left (fun map i -> Int_map.add i i map) Int_map.empty l in
let bench_map l () =
for i = 0 to n-1 do Sys.opaque_identity (ignore (Int_map.add i (-i) l)) done
@ -150,12 +184,15 @@ module L = struct
*)
and bench_batvec l () =
for i = 0 to n-1 do Sys.opaque_identity (ignore (BatVect.set l i (-i))) done
and bench_sek l () =
for i = 0 to n-1 do Sys.opaque_identity (ignore (Sek.Persistent.set l i (-i))) done
in
B.throughputN time ~repeat
[ "Map.add", bench_map map, ()
; "RAL.set", bench_ral ral, ()
(* ; "funvec.set", bench_funvec v, () *)
; "batvec.set", bench_batvec bv, ()
; "Sek.Persistent.set", bench_sek sek, ()
]
let bench_push ?(time=2) n =
@ -163,6 +200,7 @@ module L = struct
let v = ref CCFun_vec.empty in
let bv = ref BatVect.empty in
let map = ref Int_map.empty in
let sek = ref (Sek.Persistent.create 0) in
let bench_map l () =
for i = 0 to n-1 do Sys.opaque_identity (l := Int_map.add i i !l) done
(*
@ -174,19 +212,58 @@ module L = struct
for i = 0 to n-1 do Sys.opaque_identity (l := CCFun_vec.push i !l) done
and bench_batvec l () =
for i = 0 to n-1 do Sys.opaque_identity (l := BatVect.append i !l) done
and bench_sek l () =
for i = 0 to n-1 do Sys.opaque_identity (l := Sek.Persistent.push Sek.front !l i) done
in
B.throughputN time ~repeat
[ "Map.add", bench_map map, ()
(* ; "RAL.append", bench_ral ral, () *) (* too slow *)
; "Sek.Persistent.push", bench_sek sek, ()
; "funvec.push", bench_funvec v, ()
; "batvec.append", bench_batvec bv, ()
]
let bench_pop ?(time=2) n =
let l = CCList.(0 -- (n - 1)) in
let ral = CCRAL.of_list l in
let v = CCFun_vec.of_list l in
let bv = BatVect.of_list l in
let map = List.fold_left (fun map i -> Int_map.add i i map) Int_map.empty l in
let sek = Sek.Persistent.of_array 0 (Array.of_list l) in
let bench_map l () =
let l = ref l in
for i = 0 to n-1 do Sys.opaque_identity (l := Int_map.remove i !l) done
and bench_ral l () =
let l = ref l in
for _ = 0 to n-1 do Sys.opaque_identity (l := CCRAL.tl !l) done
and bench_funvec l () =
let l = ref l in
for _ = 0 to n-1 do Sys.opaque_identity (l := snd (CCFun_vec.pop_exn !l)) done
and bench_batvec l () =
let l = ref l in
for _ = 0 to n-1 do Sys.opaque_identity (l := snd (BatVect.pop !l)) done
and bench_sek l () =
let l = ref l in
for _ = 0 to n-1 do Sys.opaque_identity (l := snd (Sek.Persistent.pop Sek.back !l)) done
in
B.throughputN time ~repeat
[ "Map.remove", bench_map map, ()
; "RAL.tl", bench_ral ral, ()
; "funvec.pop", bench_funvec v, ()
; "batvec.pop", bench_batvec bv, ()
; "Sek.Persistent.pop", bench_sek sek, ()
]
(* MAIN *)
let () = B.Tree.register (
"list" @>>>
[ "map" @>>
[ "iter" @>>
B.Tree.concat
[ app_int (bench_iter ~time:2) 100
; app_int (bench_iter ~time:2) 10_000
; app_int (bench_iter ~time:4) 100_000 ]
; "map" @>>
B.Tree.concat
[ app_int (bench_map ~time:2) 100
; app_int (bench_map ~time:2) 10_000
@ -222,6 +299,11 @@ module L = struct
[ app_int (bench_push ~time:2) 100
; app_int (bench_push ~time:2) 10_000
; app_int (bench_push ~time:4) 100_000]
; "pop" @>>
B.Tree.concat
[ app_int (bench_pop ~time:2) 100
; app_int (bench_pop ~time:2) 10_000
; app_int (bench_pop ~time:4) 100_000]
]
)
end