mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
add benchmark for sorting fucntion
This commit is contained in:
parent
f7a7ce19b3
commit
3b1922671e
1 changed files with 45 additions and 1 deletions
|
|
@ -26,7 +26,7 @@ module L = struct
|
|||
let map_naive () = ignore (try List.map f_ l with Stack_overflow -> [])
|
||||
and map_tailrec () = ignore (List.rev (List.rev_map f_ l))
|
||||
and ccmap () = ignore (CCList.map f_ l)
|
||||
and ralmap () = ignore (CCRAL.map f_ ral)
|
||||
and ralmap () = ignore (CCRAL.map ~f:f_ ral)
|
||||
in
|
||||
B.throughputN time ~repeat
|
||||
[ "List.map", map_naive, ()
|
||||
|
|
@ -116,6 +116,50 @@ module L = struct
|
|||
)
|
||||
end
|
||||
|
||||
module Arr = struct
|
||||
let rand = Random.State.make [| 1;2;3;4 |]
|
||||
|
||||
let mk_arr n =
|
||||
Array.init n (fun _ -> Random.State.int rand 5_000)
|
||||
|
||||
module IntArr = struct
|
||||
type elt=int
|
||||
type t = int array
|
||||
let get = Array.get
|
||||
let set = Array.set
|
||||
let length = Array.length
|
||||
end
|
||||
|
||||
let sort_ccarray a =
|
||||
CCArray.sort_generic (module IntArr) ~cmp:CCInt.compare a
|
||||
|
||||
let sort_std a = Array.sort CCInt.compare a
|
||||
|
||||
(* helper, to apply a sort function over a list of arrays *)
|
||||
let app_list sort l =
|
||||
List.iter
|
||||
(fun a ->
|
||||
let a = Array.copy a in
|
||||
sort a
|
||||
) l
|
||||
|
||||
let bench_sort ?(time=2) n =
|
||||
let a1 = mk_arr n in
|
||||
let a2 = mk_arr n in
|
||||
let a3 = mk_arr n in
|
||||
B.throughputN time ~repeat
|
||||
[ "std", app_list sort_std, [a1;a2;a3]
|
||||
; "ccarray.sort_gen", app_list sort_ccarray, [a1;a2;a3]
|
||||
]
|
||||
|
||||
let () =
|
||||
B.Tree.register ("array" @>>>
|
||||
[ "sort" @>>
|
||||
app_ints (bench_sort ?time:None) [100; 1000; 10_000; 50_000; 100_000; 500_000]
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
module Vec = struct
|
||||
let f x = x+1
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue