mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
benchmark SkipList with hashtables
This commit is contained in:
parent
9788d108e3
commit
b4e07e07d2
1 changed files with 211 additions and 180 deletions
391
tests/benchs.ml
391
tests/benchs.ml
|
|
@ -1,5 +1,211 @@
|
||||||
|
|
||||||
(** Benchmarking *)
|
(** Benchmarking *)
|
||||||
|
(** {2 hashtables} *)
|
||||||
|
|
||||||
|
module IHashtbl = Hashtbl.Make(struct
|
||||||
|
type t = int
|
||||||
|
let equal i j = i = j
|
||||||
|
let hash i = i
|
||||||
|
end)
|
||||||
|
|
||||||
|
module IFlatHashtbl = FlatHashtbl.Make(struct
|
||||||
|
type t = int
|
||||||
|
let equal i j = i = j
|
||||||
|
let hash i = i
|
||||||
|
end)
|
||||||
|
|
||||||
|
module IFHashtbl = FHashtbl.Tree(struct
|
||||||
|
type t = int
|
||||||
|
let equal i j = i = j
|
||||||
|
let hash i = i
|
||||||
|
end)
|
||||||
|
|
||||||
|
let phashtbl_add n =
|
||||||
|
let h = PHashtbl.create 50 in
|
||||||
|
for i = n downto 0 do
|
||||||
|
PHashtbl.add h i i;
|
||||||
|
done;
|
||||||
|
h
|
||||||
|
|
||||||
|
let hashtbl_add n =
|
||||||
|
let h = Hashtbl.create 50 in
|
||||||
|
for i = n downto 0 do
|
||||||
|
Hashtbl.add h i i;
|
||||||
|
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 iflathashtbl_add n =
|
||||||
|
let h = IFlatHashtbl.create 50 in
|
||||||
|
for i = n downto 0 do
|
||||||
|
IFlatHashtbl.replace h i i;
|
||||||
|
done;
|
||||||
|
h
|
||||||
|
|
||||||
|
let ifhashtbl_add n =
|
||||||
|
let h = ref (IFHashtbl.empty 32) in
|
||||||
|
for i = n downto 0 do
|
||||||
|
h := IFHashtbl.replace !h i i;
|
||||||
|
done;
|
||||||
|
!h
|
||||||
|
|
||||||
|
let skiplist_add n =
|
||||||
|
let l = SkipList.create compare in
|
||||||
|
for i = n downto 0 do
|
||||||
|
SkipList.add l i i;
|
||||||
|
done;
|
||||||
|
l
|
||||||
|
|
||||||
|
let _ =
|
||||||
|
Format.printf "----------------------------------------@.";
|
||||||
|
let res = Bench.bench_n
|
||||||
|
["phashtbl_add", (fun n -> ignore (phashtbl_add n));
|
||||||
|
"hashtbl_add", (fun n -> ignore (hashtbl_add n));
|
||||||
|
"ihashtbl_add", (fun n -> ignore (ihashtbl_add n));
|
||||||
|
"iflathashtbl_add", (fun n -> ignore (iflathashtbl_add n));
|
||||||
|
"ifhashtbl_add", (fun n -> ignore (ifhashtbl_add n));
|
||||||
|
"skiplist_add", (fun n -> ignore (skiplist_add n));
|
||||||
|
]
|
||||||
|
in
|
||||||
|
Bench.summarize 1. res
|
||||||
|
|
||||||
|
let phashtbl_replace n =
|
||||||
|
let h = PHashtbl.create 50 in
|
||||||
|
for i = 0 to n do
|
||||||
|
PHashtbl.replace h i i;
|
||||||
|
done;
|
||||||
|
for i = n downto 0 do
|
||||||
|
PHashtbl.replace h i i;
|
||||||
|
done;
|
||||||
|
h
|
||||||
|
|
||||||
|
let hashtbl_replace n =
|
||||||
|
let h = Hashtbl.create 50 in
|
||||||
|
for i = 0 to n do
|
||||||
|
Hashtbl.replace h i i;
|
||||||
|
done;
|
||||||
|
for i = n downto 0 do
|
||||||
|
Hashtbl.replace h i i;
|
||||||
|
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 iflathashtbl_replace n =
|
||||||
|
let h = IFlatHashtbl.create 50 in
|
||||||
|
for i = 0 to n do
|
||||||
|
IFlatHashtbl.replace h i i;
|
||||||
|
done;
|
||||||
|
for i = n downto 0 do
|
||||||
|
IFlatHashtbl.replace h i i;
|
||||||
|
done;
|
||||||
|
h
|
||||||
|
|
||||||
|
let ifhashtbl_replace n =
|
||||||
|
let h = ref (IFHashtbl.empty 32) in
|
||||||
|
for i = 0 to n do
|
||||||
|
h := IFHashtbl.replace !h i i;
|
||||||
|
done;
|
||||||
|
for i = n downto 0 do
|
||||||
|
h := IFHashtbl.replace !h i i;
|
||||||
|
done;
|
||||||
|
!h
|
||||||
|
|
||||||
|
let skiplist_replace n =
|
||||||
|
let l = SkipList.create compare in
|
||||||
|
for i = 0 to n do
|
||||||
|
SkipList.add l i i;
|
||||||
|
done;
|
||||||
|
for i = n downto 0 do
|
||||||
|
SkipList.add l i i;
|
||||||
|
done;
|
||||||
|
l
|
||||||
|
|
||||||
|
let _ =
|
||||||
|
Format.printf "----------------------------------------@.";
|
||||||
|
let res = Bench.bench_n
|
||||||
|
["phashtbl_replace", (fun n -> ignore (phashtbl_replace n));
|
||||||
|
"hashtbl_replace", (fun n -> ignore (hashtbl_replace n));
|
||||||
|
"ihashtbl_replace", (fun n -> ignore (ihashtbl_replace n));
|
||||||
|
"iflathashtbl_replace", (fun n -> ignore (iflathashtbl_replace n));
|
||||||
|
"ifhashtbl_replace", (fun n -> ignore (ifhashtbl_replace n));
|
||||||
|
"skiplist_replace", (fun n -> ignore (skiplist_replace n));
|
||||||
|
]
|
||||||
|
in
|
||||||
|
Bench.summarize 1. res
|
||||||
|
|
||||||
|
let my_len = 250
|
||||||
|
let round_n n = abs ((abs n) mod my_len)
|
||||||
|
|
||||||
|
let phashtbl_find h =
|
||||||
|
fun n ->
|
||||||
|
for i = 0 to n do
|
||||||
|
ignore (PHashtbl.find h (round_n i));
|
||||||
|
done
|
||||||
|
|
||||||
|
let hashtbl_find h =
|
||||||
|
fun n ->
|
||||||
|
for i = 0 to n do
|
||||||
|
ignore (Hashtbl.find h (round_n i));
|
||||||
|
done
|
||||||
|
|
||||||
|
let ihashtbl_find h =
|
||||||
|
fun n ->
|
||||||
|
for i = 0 to n do
|
||||||
|
ignore (IHashtbl.find h (round_n i));
|
||||||
|
done
|
||||||
|
|
||||||
|
let iflathashtbl_find h =
|
||||||
|
fun n ->
|
||||||
|
for i = 0 to n do
|
||||||
|
ignore (IFlatHashtbl.find h (round_n i));
|
||||||
|
done
|
||||||
|
|
||||||
|
let ifhashtbl_find h =
|
||||||
|
fun n ->
|
||||||
|
for i = 0 to n do
|
||||||
|
ignore (IFHashtbl.find h (round_n i));
|
||||||
|
done
|
||||||
|
|
||||||
|
let skiplist_find l =
|
||||||
|
fun n ->
|
||||||
|
for i = 0 to n do
|
||||||
|
ignore (SkipList.find l (round_n i));
|
||||||
|
done
|
||||||
|
|
||||||
|
let _ =
|
||||||
|
let h = phashtbl_add my_len in
|
||||||
|
let h' = hashtbl_add my_len in
|
||||||
|
let h'' = ihashtbl_add my_len in
|
||||||
|
let h''' = iflathashtbl_add my_len in
|
||||||
|
let h'''' = ifhashtbl_add my_len in
|
||||||
|
let l = skiplist_add my_len in
|
||||||
|
List.iter (fun n ->
|
||||||
|
Format.printf "----------------------------------------@.";
|
||||||
|
Format.printf "try on size %d@.@.@." n;
|
||||||
|
Bench.bench [
|
||||||
|
"phashtbl_find", (fun () -> phashtbl_find h n);
|
||||||
|
"hashtbl_find", (fun () -> hashtbl_find h' n);
|
||||||
|
"ihashtbl_find", (fun () -> ihashtbl_find h'' n);
|
||||||
|
"iflathashtbl_find", (fun () -> iflathashtbl_find h''' n);
|
||||||
|
"ifhashtbl_find", (fun () -> ifhashtbl_find h'''' n);
|
||||||
|
"skiplist_find", (fun () -> skiplist_find l n);
|
||||||
|
])
|
||||||
|
[10;20;100;1000;10000;100000]
|
||||||
|
|
||||||
(** {2 Sequence/Gen} *)
|
(** {2 Sequence/Gen} *)
|
||||||
|
|
||||||
|
|
@ -77,189 +283,14 @@ let _ =
|
||||||
ignore (List.map fib [5;10;20;30;35]);
|
ignore (List.map fib [5;10;20;30;35]);
|
||||||
()
|
()
|
||||||
in
|
in
|
||||||
|
let conf = Bench.config in
|
||||||
|
conf.Bench.samples <- 100;
|
||||||
Bench.bench
|
Bench.bench
|
||||||
[ "linear_fib", bench_fib (LinearFibo.fib ~size:5);
|
[ "linear_fib", bench_fib (LinearFibo.fib ~size:5);
|
||||||
"replacing_fib", bench_fib (ReplacingFibo.fib ~size:256);
|
"replacing_fib", bench_fib (ReplacingFibo.fib ~size:256);
|
||||||
"LRU_fib", bench_fib (LRUFibo.fib ~size:256);
|
"LRU_fib", bench_fib (LRUFibo.fib ~size:256);
|
||||||
"dummy_fib", bench_fib (DummyFibo.fib ~size:5);
|
"dummy_fib", bench_fib (DummyFibo.fib ~size:5);
|
||||||
]
|
];
|
||||||
|
conf.Bench.samples <- 1000;
|
||||||
|
()
|
||||||
|
|
||||||
(** {2 hashtables} *)
|
|
||||||
|
|
||||||
module IHashtbl = Hashtbl.Make(struct
|
|
||||||
type t = int
|
|
||||||
let equal i j = i = j
|
|
||||||
let hash i = i
|
|
||||||
end)
|
|
||||||
|
|
||||||
module IFlatHashtbl = FlatHashtbl.Make(struct
|
|
||||||
type t = int
|
|
||||||
let equal i j = i = j
|
|
||||||
let hash i = i
|
|
||||||
end)
|
|
||||||
|
|
||||||
module IFHashtbl = FHashtbl.Tree(struct
|
|
||||||
type t = int
|
|
||||||
let equal i j = i = j
|
|
||||||
let hash i = i
|
|
||||||
end)
|
|
||||||
|
|
||||||
let phashtbl_add n =
|
|
||||||
let h = PHashtbl.create 50 in
|
|
||||||
for i = n downto 0 do
|
|
||||||
PHashtbl.add h i i;
|
|
||||||
done;
|
|
||||||
h
|
|
||||||
|
|
||||||
let hashtbl_add n =
|
|
||||||
let h = Hashtbl.create 50 in
|
|
||||||
for i = n downto 0 do
|
|
||||||
Hashtbl.add h i i;
|
|
||||||
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 iflathashtbl_add n =
|
|
||||||
let h = IFlatHashtbl.create 50 in
|
|
||||||
for i = n downto 0 do
|
|
||||||
IFlatHashtbl.replace h i i;
|
|
||||||
done;
|
|
||||||
h
|
|
||||||
|
|
||||||
let ifhashtbl_add n =
|
|
||||||
let h = ref (IFHashtbl.empty 32) in
|
|
||||||
for i = n downto 0 do
|
|
||||||
h := IFHashtbl.replace !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));
|
|
||||||
"ihashtbl_add", (fun n -> ignore (ihashtbl_add n));
|
|
||||||
"iflathashtbl_add", (fun n -> ignore (iflathashtbl_add n));
|
|
||||||
"ifhashtbl_add", (fun n -> ignore (ifhashtbl_add n));
|
|
||||||
]
|
|
||||||
in
|
|
||||||
Bench.summarize 1. res
|
|
||||||
|
|
||||||
let phashtbl_replace n =
|
|
||||||
let h = PHashtbl.create 50 in
|
|
||||||
for i = 0 to n do
|
|
||||||
PHashtbl.replace h i i;
|
|
||||||
done;
|
|
||||||
for i = n downto 0 do
|
|
||||||
PHashtbl.replace h i i;
|
|
||||||
done;
|
|
||||||
h
|
|
||||||
|
|
||||||
let hashtbl_replace n =
|
|
||||||
let h = Hashtbl.create 50 in
|
|
||||||
for i = 0 to n do
|
|
||||||
Hashtbl.replace h i i;
|
|
||||||
done;
|
|
||||||
for i = n downto 0 do
|
|
||||||
Hashtbl.replace h i i;
|
|
||||||
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 iflathashtbl_replace n =
|
|
||||||
let h = IFlatHashtbl.create 50 in
|
|
||||||
for i = 0 to n do
|
|
||||||
IFlatHashtbl.replace h i i;
|
|
||||||
done;
|
|
||||||
for i = n downto 0 do
|
|
||||||
IFlatHashtbl.replace h i i;
|
|
||||||
done;
|
|
||||||
h
|
|
||||||
|
|
||||||
let ifhashtbl_replace n =
|
|
||||||
let h = ref (IFHashtbl.empty 32) in
|
|
||||||
for i = 0 to n do
|
|
||||||
h := IFHashtbl.replace !h i i;
|
|
||||||
done;
|
|
||||||
for i = n downto 0 do
|
|
||||||
h := IFHashtbl.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));
|
|
||||||
"ihashtbl_replace", (fun n -> ignore (ihashtbl_replace n));
|
|
||||||
"iflathashtbl_replace", (fun n -> ignore (iflathashtbl_replace n));
|
|
||||||
"ifhashtbl_replace", (fun n -> ignore (ifhashtbl_replace n));
|
|
||||||
]
|
|
||||||
in
|
|
||||||
Bench.summarize 1. res
|
|
||||||
|
|
||||||
let my_len = 250
|
|
||||||
let round_n n = abs ((abs n) mod my_len)
|
|
||||||
|
|
||||||
let phashtbl_find h =
|
|
||||||
fun n ->
|
|
||||||
for i = 0 to n do
|
|
||||||
ignore (PHashtbl.find h (round_n i));
|
|
||||||
done
|
|
||||||
|
|
||||||
let hashtbl_find h =
|
|
||||||
fun n ->
|
|
||||||
for i = 0 to n do
|
|
||||||
ignore (Hashtbl.find h (round_n i));
|
|
||||||
done
|
|
||||||
|
|
||||||
let ihashtbl_find h =
|
|
||||||
fun n ->
|
|
||||||
for i = 0 to n do
|
|
||||||
ignore (IHashtbl.find h (round_n i));
|
|
||||||
done
|
|
||||||
|
|
||||||
let iflathashtbl_find h =
|
|
||||||
fun n ->
|
|
||||||
for i = 0 to n do
|
|
||||||
ignore (IFlatHashtbl.find h (round_n i));
|
|
||||||
done
|
|
||||||
|
|
||||||
let ifhashtbl_find h =
|
|
||||||
fun n ->
|
|
||||||
for i = 0 to n do
|
|
||||||
ignore (IFHashtbl.find h (round_n i));
|
|
||||||
done
|
|
||||||
|
|
||||||
let _ =
|
|
||||||
let h = phashtbl_add my_len in
|
|
||||||
let h' = hashtbl_add my_len in
|
|
||||||
let h'' = ihashtbl_add my_len in
|
|
||||||
let h''' = iflathashtbl_add my_len in
|
|
||||||
let h'''' = ifhashtbl_add my_len in
|
|
||||||
List.iter (fun n ->
|
|
||||||
Format.printf "----------------------------------------@.";
|
|
||||||
Format.printf "try on size %d@.@.@." n;
|
|
||||||
Bench.bench [
|
|
||||||
"phashtbl_find", (fun () -> phashtbl_find h n);
|
|
||||||
"hashtbl_find", (fun () -> hashtbl_find h' n);
|
|
||||||
"ihashtbl_find", (fun () -> ihashtbl_find h'' n);
|
|
||||||
"iflathashtbl_find", (fun () -> iflathashtbl_find h''' n);
|
|
||||||
"ifhashtbl_find", (fun () -> ifhashtbl_find h'''' n);
|
|
||||||
])
|
|
||||||
[10;20;100;1000;10000;100000]
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue