Skip to content

Instantly share code, notes, and snippets.

@alainfrisch
Last active March 8, 2017 12:31
Show Gist options
  • Select an option

  • Save alainfrisch/2cbb615d85cbf86e22c59e15dbcc260f to your computer and use it in GitHub Desktop.

Select an option

Save alainfrisch/2cbb615d85cbf86e22c59e15dbcc260f to your computer and use it in GitHub Desktop.
bench_set
let cmps = ref 0
module I = Set.Make(struct
type t = int
let compare (x : t) y = incr cmps; compare x y
end)
let test n =
let i0 = 10000000 in
let rec mk a i =
if i = 0 then a
else mk (I.add (i0 + i) a) (i - 1)
in
let s0 = mk I.empty n in
Gc.compact ();
let t0 = Unix.gettimeofday () in
for i = 1 to 100000 do
cmps := 0;
let s = ref s0 in
for j = 1 to 10 do
s := I.add j !s
done;
for j = 1 to 10 do
ignore (I.mem j !s);
done
done;
let t1 = Unix.gettimeofday () in
Printf.printf "% 7i: %.02f % 8i\n%!" n (t1 -. t0) !cmps
let () =
List.iter test [0; 1; 10; 100; 1000; 10000; 100000]
0: 0.06s 60 comparisons
1: 0.07s 66 comparisons
10: 0.09s 97 comparisons
100: 0.14s 143 comparisons
1000: 0.20s 200 comparisons
10000: 0.27s 280 comparisons
100000: 0.33s 340 comparisons
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment