Skip to content

Instantly share code, notes, and snippets.

@bcc32
Last active September 5, 2019 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcc32/98e30ba0cecdd953572139a16d6a2e6c to your computer and use it in GitHub Desktop.
Save bcc32/98e30ba0cecdd953572139a16d6a2e6c to your computer and use it in GitHub Desktop.
Benchmark of Map.length between OCaml stdlib and Jane Street Base/Core
(* https://discuss.ocaml.org/t/efficiently-checking-if-map-set-is-singleton/1611/2 *)
open! Core
open Core_bench
let bench = Bench.Test.create_indexed ~args:[ 1; 10; 100; 1000; 10_000 ]
module type Map = sig
type 'data t
val empty : _ t
val add : 'data t -> key:int -> data:'data -> 'data t
val length : _ t -> int
end
module type S = sig
include Map
val create : len:int -> int t
end
module Make(M : Map) : S = struct
let create ~len =
Sequence.range 0 len
|> Sequence.fold ~init:M.empty ~f:(fun m x ->
M.add m ~key:x ~data:x)
;;
include M
end
module Stdlib_int_map : Map = struct
module M = Caml.Map.Make(Int)
type 'data t = 'data M.t
let empty = M.empty
let length = M.cardinal
let add t ~key ~data = M.add key data t
end
let bench_map_length (module Map : S) ~name =
bench ~name
(fun len ->
let m = Map.create ~len in
stage (fun () -> Map.length m))
;;
let stdlib =
let module Map = Make(Stdlib_int_map) in
bench_map_length ~name:"stdlib" (module Map)
;;
let core =
let module Map = Make(Int.Map) in
bench_map_length ~name:"core" (module Map)
;;
let () =
Bench.make_command
[ stdlib
; core ]
|> Command.run
;;
(executable
((name bench_map_length)
(libraries (core core_bench))
(preprocess (pps (ppx_jane)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment