mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
More benchmarks for immutable vectors
- Add Map as a reference, as this is the only efficient implementation in OCaml's standard library - Add Clarity.Vector from the clarity library - New benchmarks for set at index and push What's missing: - CCFun_vec.set (no implementation) - CCRAL.push and Clarity.Vector.push were missing and implemented inefficiently in terms of append
This commit is contained in:
parent
1adfc01cf0
commit
35065393c5
2 changed files with 76 additions and 2 deletions
|
|
@ -3,7 +3,7 @@
|
||||||
((names (run_benchs run_bench_hash))
|
((names (run_benchs run_bench_hash))
|
||||||
(libraries (containers containers.data containers.iter
|
(libraries (containers containers.data containers.iter
|
||||||
containers.thread benchmark gen sequence qcheck
|
containers.thread benchmark gen sequence qcheck
|
||||||
batteries))
|
batteries clarity))
|
||||||
(flags (:standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string -color always))
|
(flags (:standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string -color always))
|
||||||
(ocamlopt_flags (:standard -O3 -color always
|
(ocamlopt_flags (:standard -O3 -color always
|
||||||
-unbox-closures -unbox-closures-factor 20))
|
-unbox-closures -unbox-closures-factor 20))
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ let (@>>) = B.Tree.(@>>)
|
||||||
let (@>>>) = B.Tree.(@>>>)
|
let (@>>>) = B.Tree.(@>>>)
|
||||||
let (|>) = CCFun.(|>)
|
let (|>) = CCFun.(|>)
|
||||||
|
|
||||||
|
module Int_map = Map.Make(CCInt)
|
||||||
|
|
||||||
let app_int f n = string_of_int n @> lazy (f n)
|
let app_int f n = string_of_int n @> lazy (f n)
|
||||||
let app_ints f l = B.Tree.concat (List.map (app_int f) l)
|
let app_ints f l = B.Tree.concat (List.map (app_int f) l)
|
||||||
|
|
||||||
|
|
@ -108,24 +110,86 @@ module L = struct
|
||||||
(* RANDOM ACCESS *)
|
(* RANDOM ACCESS *)
|
||||||
|
|
||||||
let bench_nth ?(time=2) n =
|
let bench_nth ?(time=2) n =
|
||||||
let l = CCList.(1 -- n) in
|
let l = CCList.(0 -- (n - 1)) in
|
||||||
let ral = CCRAL.of_list l in
|
let ral = CCRAL.of_list l in
|
||||||
let v = CCFun_vec.of_list l in
|
let v = CCFun_vec.of_list l in
|
||||||
let bv = BatVect.of_list l in
|
let bv = BatVect.of_list l in
|
||||||
|
let cv = Clarity.Vector.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_list l () =
|
let bench_list l () =
|
||||||
for i = 0 to n-1 do Sys.opaque_identity (ignore (List.nth l i)) done
|
for i = 0 to n-1 do Sys.opaque_identity (ignore (List.nth l i)) done
|
||||||
|
and bench_map l () =
|
||||||
|
for i = 0 to n-1 do Sys.opaque_identity (ignore (Int_map.find i l)) done
|
||||||
and bench_ral l () =
|
and bench_ral l () =
|
||||||
for i = 0 to n-1 do Sys.opaque_identity (ignore (CCRAL.get_exn l i)) done
|
for i = 0 to n-1 do Sys.opaque_identity (ignore (CCRAL.get_exn l i)) done
|
||||||
and bench_funvec l () =
|
and bench_funvec l () =
|
||||||
for i = 0 to n-1 do Sys.opaque_identity (ignore (CCFun_vec.get_exn i l)) done
|
for i = 0 to n-1 do Sys.opaque_identity (ignore (CCFun_vec.get_exn i l)) done
|
||||||
and bench_batvec l () =
|
and bench_batvec l () =
|
||||||
for i = 0 to n-1 do Sys.opaque_identity (ignore (BatVect.get l i)) done
|
for i = 0 to n-1 do Sys.opaque_identity (ignore (BatVect.get l i)) done
|
||||||
|
and bench_clarity_vec l () =
|
||||||
|
for i = 0 to n-1 do Sys.opaque_identity (ignore (Clarity.Vector.get l i)) done
|
||||||
in
|
in
|
||||||
B.throughputN time ~repeat
|
B.throughputN time ~repeat
|
||||||
[ "List.nth", bench_list l, ()
|
[ "List.nth", bench_list l, ()
|
||||||
|
; "Map.find", bench_map map, ()
|
||||||
; "RAL.get", bench_ral ral, ()
|
; "RAL.get", bench_ral ral, ()
|
||||||
; "funvec.get", bench_funvec v, ()
|
; "funvec.get", bench_funvec v, ()
|
||||||
; "batvec.get", bench_batvec bv, ()
|
; "batvec.get", bench_batvec bv, ()
|
||||||
|
; "clarity_vec.get", bench_clarity_vec cv, ()
|
||||||
|
]
|
||||||
|
|
||||||
|
let bench_set ?(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 cv = Clarity.Vector.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
|
||||||
|
and bench_ral l () =
|
||||||
|
for i = 0 to n-1 do Sys.opaque_identity (ignore (CCRAL.set l i (-i))) done
|
||||||
|
and bench_funvec l () =
|
||||||
|
for i = 0 to n-1 do Sys.opaque_identity (ignore ((* TODO *))) done
|
||||||
|
and bench_batvec l () =
|
||||||
|
for i = 0 to n-1 do Sys.opaque_identity (ignore (BatVect.set l i (-i))) done
|
||||||
|
and bench_clarity_vec l () =
|
||||||
|
for i = 0 to n-1 do Sys.opaque_identity (ignore (Clarity.Vector.update l i (-1))) 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, ()
|
||||||
|
; "clarity_vec.update", bench_clarity_vec cv, ()
|
||||||
|
]
|
||||||
|
|
||||||
|
let bench_push ?(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 cv = Clarity.Vector.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
|
||||||
|
and bench_ral l () =
|
||||||
|
(* Note: Better implementation probably possible *)
|
||||||
|
for i = 0 to n-1 do Sys.opaque_identity (ignore (CCRAL.append l (CCRAL.return i))) done
|
||||||
|
and bench_funvec l () =
|
||||||
|
for i = 0 to n-1 do Sys.opaque_identity (ignore (CCFun_vec.push i l)) done
|
||||||
|
and bench_batvec l () =
|
||||||
|
for i = 0 to n-1 do Sys.opaque_identity (ignore (BatVect.append i l)) done
|
||||||
|
and bench_clarity_vec l () =
|
||||||
|
(* Note: Better implementation probably possible *)
|
||||||
|
for i = 0 to n-1 do Sys.opaque_identity (ignore (Clarity.Vector.append l (Clarity.Vector.pure i))) done
|
||||||
|
in
|
||||||
|
B.throughputN time ~repeat
|
||||||
|
[ "Map.add", bench_map map, ()
|
||||||
|
(* ; "RAL.append", bench_ral ral, () *) (* too slow *)
|
||||||
|
; "funvec.push", bench_funvec v, ()
|
||||||
|
; "batvec.append", bench_batvec bv, ()
|
||||||
|
; "clarity_vec.append", bench_clarity_vec cv, ()
|
||||||
]
|
]
|
||||||
|
|
||||||
(* MAIN *)
|
(* MAIN *)
|
||||||
|
|
@ -158,6 +222,16 @@ module L = struct
|
||||||
[ app_int (bench_nth ~time:2) 100
|
[ app_int (bench_nth ~time:2) 100
|
||||||
; app_int (bench_nth ~time:2) 10_000
|
; app_int (bench_nth ~time:2) 10_000
|
||||||
; app_int (bench_nth ~time:4) 100_000]
|
; app_int (bench_nth ~time:4) 100_000]
|
||||||
|
; "set" @>>
|
||||||
|
B.Tree.concat
|
||||||
|
[ app_int (bench_set ~time:2) 100
|
||||||
|
; app_int (bench_set ~time:2) 10_000
|
||||||
|
; app_int (bench_set ~time:4) 100_000]
|
||||||
|
; "push" @>>
|
||||||
|
B.Tree.concat
|
||||||
|
[ app_int (bench_push ~time:2) 100
|
||||||
|
; app_int (bench_push ~time:2) 10_000
|
||||||
|
; app_int (bench_push ~time:4) 100_000]
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue