mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-05 19:00:31 -05:00
also benchmark IHashtbl (functor);
use Bench.bench_n, which is much better
This commit is contained in:
parent
f3074584ff
commit
f7d0d88882
1 changed files with 48 additions and 17 deletions
|
|
@ -1,6 +1,12 @@
|
|||
|
||||
(** Benchmarking *)
|
||||
|
||||
module IHashtbl = Hashtbl.Make(struct
|
||||
type t = int
|
||||
let equal i j = i - j = 0
|
||||
let hash i = i
|
||||
end)
|
||||
|
||||
let phashtbl_add n =
|
||||
let h = PHashtbl.create 50 in
|
||||
for i = n downto 0 do
|
||||
|
|
@ -15,10 +21,19 @@ let hashtbl_add n =
|
|||
done;
|
||||
h
|
||||
|
||||
let ihashtbl_add n =
|
||||
let h = IHashtbl.create 50 in
|
||||
for i = n downto 0 do
|
||||
IHashtbl.add h i i;
|
||||
done;
|
||||
h
|
||||
|
||||
let _ =
|
||||
Format.printf "----------------------------------------@.";
|
||||
let res = Bench.bench_n
|
||||
["phashtbl_add", (fun n -> ignore (phashtbl_add n));
|
||||
"hashtbl_add", (fun n -> ignore (hashtbl_add n));]
|
||||
"hashtbl_add", (fun n -> ignore (hashtbl_add n));
|
||||
"ihashtbl_add", (fun n -> ignore (ihashtbl_add n));]
|
||||
in
|
||||
Bench.summarize 1. res
|
||||
|
||||
|
|
@ -42,32 +57,48 @@ let hashtbl_replace n =
|
|||
done;
|
||||
h
|
||||
|
||||
let ihashtbl_replace n =
|
||||
let h = IHashtbl.create 50 in
|
||||
for i = 0 to n do
|
||||
IHashtbl.replace h i i;
|
||||
done;
|
||||
for i = n downto 0 do
|
||||
IHashtbl.replace h i i;
|
||||
done;
|
||||
h
|
||||
|
||||
let _ =
|
||||
Format.printf "----------------------------------------@.";
|
||||
let res = Bench.bench_n
|
||||
["phashtbl_replace", (fun n -> ignore (phashtbl_replace n));
|
||||
"hashtbl_replace", (fun n -> ignore (hashtbl_replace n));]
|
||||
"hashtbl_replace", (fun n -> ignore (hashtbl_replace n));
|
||||
"ihashtbl_replace", (fun n -> ignore (ihashtbl_replace n));]
|
||||
in
|
||||
Bench.summarize 1. res
|
||||
|
||||
let phashtbl_mem h =
|
||||
let my_len = 250
|
||||
let round_n n = abs (n mod my_len)
|
||||
|
||||
let phashtbl_find h =
|
||||
fun n ->
|
||||
for i = 0 to n do
|
||||
ignore (PHashtbl.find h i);
|
||||
done
|
||||
ignore (PHashtbl.find h (round_n n))
|
||||
|
||||
let hashtbl_mem h =
|
||||
let hashtbl_find h =
|
||||
fun n ->
|
||||
for i = 0 to n do
|
||||
ignore (Hashtbl.find h i);
|
||||
done
|
||||
ignore (Hashtbl.find h (round_n n))
|
||||
|
||||
let ihashtbl_find h =
|
||||
fun n ->
|
||||
ignore (IHashtbl.find h (round_n n))
|
||||
|
||||
let _ =
|
||||
let n = 50000 in
|
||||
let h = phashtbl_add n in
|
||||
let h' = hashtbl_add n in
|
||||
let res = Bench.bench_funs
|
||||
["phashtbl_mem", phashtbl_mem h;
|
||||
"hashtbl_mem", hashtbl_mem h';]
|
||||
n
|
||||
Format.printf "----------------------------------------@.";
|
||||
let h = phashtbl_add my_len in
|
||||
let h' = hashtbl_add my_len in
|
||||
let h'' = ihashtbl_add my_len in
|
||||
let res = Bench.bench_n
|
||||
["phashtbl_mem", phashtbl_find h;
|
||||
"hashtbl_mem", hashtbl_find h';
|
||||
"ihashtbl_mem", ihashtbl_find h'';]
|
||||
in
|
||||
Bench.summarize 1. res
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue