Skip to content

Instantly share code, notes, and snippets.

@mooreryan
Last active September 15, 2022 01:33
Show Gist options
  • Save mooreryan/8e74aed315f9a96d434967ef8632ab9d to your computer and use it in GitHub Desktop.
Save mooreryan/8e74aed315f9a96d434967ef8632ab9d to your computer and use it in GitHub Desktop.
OCaml equal benchmark with variants
[@@@warning "-37"]
open! Core
open! Core_bench
module Nucleotide = struct
type t = A | C | G | T
let poly_equal = Poly.( = )
let spec_equal : t -> t -> bool = fun x y -> Poly.(x = y)
let match_equal x y =
match (x, y) with A, A | C, C | G, G | T, T -> true | _ -> false
end
let a = Nucleotide.A
let t = Nucleotide.T
let () =
Command_unix.run
(Bench.make_command
[
Bench.Test.create ~name:"poly" (fun () ->
Nucleotide.poly_equal a a && Nucleotide.poly_equal a t);
Bench.Test.create ~name:"spec" (fun () ->
Nucleotide.spec_equal a a && Nucleotide.spec_equal a t);
Bench.Test.create ~name:"match" (fun () ->
Nucleotide.match_equal a a && Nucleotide.match_equal a t);
])
(executable
(name bench_equal)
(libraries core core_bench core_unix.command_unix))

Results

$ dune exec ./bench/bench_equal.exe
Estimated testing time 30s (3 benchmarks x 10s). Change using '-quota'.
┌───────┬──────────┬────────────┐
│ Name  │ Time/Run │ Percentage │
├───────┼──────────┼────────────┤
│ poly  │  11.84ns │    100.00% │
│ spec  │   1.51ns │     12.77% │
│ match │   4.03ns │     34.06% │
└───────┴──────────┴────────────┘
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment