mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 19:25:28 -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 *)
|
(** 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 phashtbl_add n =
|
||||||
let h = PHashtbl.create 50 in
|
let h = PHashtbl.create 50 in
|
||||||
for i = n downto 0 do
|
for i = n downto 0 do
|
||||||
|
|
@ -15,10 +21,19 @@ let hashtbl_add n =
|
||||||
done;
|
done;
|
||||||
h
|
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 _ =
|
let _ =
|
||||||
|
Format.printf "----------------------------------------@.";
|
||||||
let res = Bench.bench_n
|
let res = Bench.bench_n
|
||||||
["phashtbl_add", (fun n -> ignore (phashtbl_add 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
|
in
|
||||||
Bench.summarize 1. res
|
Bench.summarize 1. res
|
||||||
|
|
||||||
|
|
@ -42,32 +57,48 @@ let hashtbl_replace n =
|
||||||
done;
|
done;
|
||||||
h
|
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 _ =
|
let _ =
|
||||||
|
Format.printf "----------------------------------------@.";
|
||||||
let res = Bench.bench_n
|
let res = Bench.bench_n
|
||||||
["phashtbl_replace", (fun n -> ignore (phashtbl_replace 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
|
in
|
||||||
Bench.summarize 1. res
|
Bench.summarize 1. res
|
||||||
|
|
||||||
let phashtbl_mem h =
|
let my_len = 250
|
||||||
fun n ->
|
let round_n n = abs (n mod my_len)
|
||||||
for i = 0 to n do
|
|
||||||
ignore (PHashtbl.find h i);
|
|
||||||
done
|
|
||||||
|
|
||||||
let hashtbl_mem h =
|
let phashtbl_find h =
|
||||||
fun n ->
|
fun n ->
|
||||||
for i = 0 to n do
|
ignore (PHashtbl.find h (round_n n))
|
||||||
ignore (Hashtbl.find h i);
|
|
||||||
done
|
let hashtbl_find h =
|
||||||
|
fun n ->
|
||||||
|
ignore (Hashtbl.find h (round_n n))
|
||||||
|
|
||||||
|
let ihashtbl_find h =
|
||||||
|
fun n ->
|
||||||
|
ignore (IHashtbl.find h (round_n n))
|
||||||
|
|
||||||
let _ =
|
let _ =
|
||||||
let n = 50000 in
|
Format.printf "----------------------------------------@.";
|
||||||
let h = phashtbl_add n in
|
let h = phashtbl_add my_len in
|
||||||
let h' = hashtbl_add n in
|
let h' = hashtbl_add my_len in
|
||||||
let res = Bench.bench_funs
|
let h'' = ihashtbl_add my_len in
|
||||||
["phashtbl_mem", phashtbl_mem h;
|
let res = Bench.bench_n
|
||||||
"hashtbl_mem", hashtbl_mem h';]
|
["phashtbl_mem", phashtbl_find h;
|
||||||
n
|
"hashtbl_mem", hashtbl_find h';
|
||||||
|
"ihashtbl_mem", ihashtbl_find h'';]
|
||||||
in
|
in
|
||||||
Bench.summarize 1. res
|
Bench.summarize 1. res
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue