From 35065393c5df77a3dd3f9d1a976ec026b64ca3b0 Mon Sep 17 00:00:00 2001 From: Fabian Date: Thu, 31 May 2018 16:35:21 -0500 Subject: [PATCH] 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 --- benchs/jbuild | 2 +- benchs/run_benchs.ml | 76 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/benchs/jbuild b/benchs/jbuild index c0c4c2f4..5f81f2ef 100644 --- a/benchs/jbuild +++ b/benchs/jbuild @@ -3,7 +3,7 @@ ((names (run_benchs run_bench_hash)) (libraries (containers containers.data containers.iter 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)) (ocamlopt_flags (:standard -O3 -color always -unbox-closures -unbox-closures-factor 20)) diff --git a/benchs/run_benchs.ml b/benchs/run_benchs.ml index 93e96782..ec390261 100644 --- a/benchs/run_benchs.ml +++ b/benchs/run_benchs.ml @@ -6,6 +6,8 @@ let (@>>) = B.Tree.(@>>) let (@>>>) = B.Tree.(@>>>) let (|>) = CCFun.(|>) +module Int_map = Map.Make(CCInt) + 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) @@ -108,24 +110,86 @@ module L = struct (* RANDOM ACCESS *) 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 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_list l () = 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 () = for i = 0 to n-1 do Sys.opaque_identity (ignore (CCRAL.get_exn l i)) done and bench_funvec l () = 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_clarity_vec l () = + for i = 0 to n-1 do Sys.opaque_identity (ignore (Clarity.Vector.get l i)) done in B.throughputN time ~repeat [ "List.nth", bench_list l, () + ; "Map.find", bench_map map, () ; "RAL.get", bench_ral ral, () ; "funvec.get", bench_funvec v, () ; "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 *) @@ -158,6 +222,16 @@ module L = struct [ app_int (bench_nth ~time:2) 100 ; app_int (bench_nth ~time:2) 10_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