Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bench/bench_ocaml/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/_build
2 changes: 2 additions & 0 deletions bench/bench_ocaml/.ocamlformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
profile = default
version = 0.21.0
40 changes: 40 additions & 0 deletions bench/bench_ocaml/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Benchmarking in OCaml

## Build in watch mode

```shell
dune build --watch --terminal-persistence clear-on-rebuild
```

## Run tests in watch mode

```shell
dune runtest --watch --terminal-persistence clear-on-rebuild
```

Run benchmarks

```shell
dune exec bench_ocaml
```

## Results of benchmarking

```
Estimated testing time 20s (2 benchmarks x 10s). Change using '-quota'.
┌──────────────┬────────────┬─────────┬────────────┐
│ Name │ Time/Run │ mWd/Run │ Percentage │
├──────────────┼────────────┼─────────┼────────────┤
│ Float lib │ 4_030.64ns │ 456.00w │ 100.00% │
│ Rational lib │ 660.22ns │ 45.00w │ 16.38% │
└──────────────┴────────────┴─────────┴────────────┘
Estimated testing time 20s (2 benchmarks x 10s). Change using '-quota'.
┌──────────────────────────┬──────────────┬──────────┬───────────────┬───────────┬────────────┐
│ Name │ Time/Run │ mWd/Run │ mjWd/Run │ Prom/Run │ Percentage │
├──────────────────────────┼──────────────┼──────────┼───────────────┼───────────┼────────────┤
│ Trigonomery Float lig │ 231.34us │ 40.82kw │ 9.03w │ 9.03w │ 0.06% │
│ Trigonomery Rational lig │ 396_345.95us │ 132.45kw │ 8_752_685.82w │ 2_177.82w │ 100.00% │
└──────────────────────────┴──────────────┴──────────┴───────────────┴───────────┴────────────┘
Benchmarks that take 1ns to 100ms can be estimated precisely. For more reliable
estimates, redesign your benchmark to have a shorter execution time.
```
31 changes: 31 additions & 0 deletions bench/bench_ocaml/bench_ocaml.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This file is generated by dune, edit dune-project instead
opam-version: "2.0"
maintainer: ["Maintainer Name"]
authors: ["ligolang"]
license: "LICENSE"
homepage: "https://github.com/ligolang/math-lib-cameligo"
doc: "https://url/to/documentation"
bug-reports: "https://github.com/ligolang/math-lib-cameligo/issues"
depends: [
"ocaml"
"dune" {>= "3.4"}
"core"
"core_bench"
"zarith"
"odoc" {with-doc}
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
dev-repo: "git+https://github.com/ligolang/math-lib-cameligo.git"
107 changes: 107 additions & 0 deletions bench/bench_ocaml/bench_ocaml.opam.locked
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
opam-version: "2.0"
name: "bench_ocaml"
version: "~dev"
maintainer: "Maintainer Name"
authors: "ligolang"
license: "LICENSE"
homepage: "https://github.com/ligolang/math-lib-cameligo"
doc: "https://url/to/documentation"
bug-reports: "https://github.com/ligolang/math-lib-cameligo/issues"
depends: [
"base" {= "v0.15.0"}
"base-bigarray" {= "base"}
"base-threads" {= "base"}
"base-unix" {= "base"}
"base_bigstring" {= "v0.15.0"}
"base_quickcheck" {= "v0.15.0"}
"bin_prot" {= "v0.15.0"}
"cmdliner" {= "1.1.1"}
"conf-gmp" {= "4"}
"core" {= "v0.15.0"}
"core_bench" {= "v0.15.0"}
"core_kernel" {= "v0.15.0"}
"core_unix" {= "v0.15.0"}
"csexp" {= "1.5.1"}
"dune" {= "3.4.1"}
"dune-configurator" {= "3.4.1"}
"expect_test_helpers_core" {= "v0.15.0"}
"fieldslib" {= "v0.15.0"}
"int_repr" {= "v0.15.0"}
"jane-street-headers" {= "v0.15.0"}
"jst-config" {= "v0.15.1"}
"num" {= "1.4"}
"ocaml" {= "4.14.0"}
"ocaml-base-compiler" {= "4.14.0"}
"ocaml-compiler-libs" {= "v0.12.4"}
"ocaml-config" {= "2"}
"ocaml-options-vanilla" {= "1"}
"ocaml_intrinsics" {= "v0.15.2"}
"ocamlbuild" {= "0.14.1"}
"ocamlfind" {= "1.9.5"}
"parsexp" {= "v0.15.0"}
"ppx_assert" {= "v0.15.0"}
"ppx_base" {= "v0.15.0"}
"ppx_bench" {= "v0.15.0"}
"ppx_bin_prot" {= "v0.15.0"}
"ppx_cold" {= "v0.15.0"}
"ppx_compare" {= "v0.15.0"}
"ppx_custom_printf" {= "v0.15.0"}
"ppx_derivers" {= "1.2.1"}
"ppx_disable_unused_warnings" {= "v0.15.0"}
"ppx_enumerate" {= "v0.15.0"}
"ppx_expect" {= "v0.15.0"}
"ppx_fields_conv" {= "v0.15.0"}
"ppx_fixed_literal" {= "v0.15.0"}
"ppx_hash" {= "v0.15.0"}
"ppx_here" {= "v0.15.0"}
"ppx_ignore_instrumentation" {= "v0.15.0"}
"ppx_inline_test" {= "v0.15.0"}
"ppx_jane" {= "v0.15.0"}
"ppx_let" {= "v0.15.0"}
"ppx_log" {= "v0.15.0"}
"ppx_module_timer" {= "v0.15.0"}
"ppx_optcomp" {= "v0.15.0"}
"ppx_optional" {= "v0.15.0"}
"ppx_pipebang" {= "v0.15.0"}
"ppx_sexp_conv" {= "v0.15.1"}
"ppx_sexp_message" {= "v0.15.0"}
"ppx_sexp_value" {= "v0.15.0"}
"ppx_stable" {= "v0.15.0"}
"ppx_string" {= "v0.15.0"}
"ppx_typerep_conv" {= "v0.15.0"}
"ppx_variants_conv" {= "v0.15.0"}
"ppxlib" {= "0.27.0"}
"re" {= "1.10.4"}
"seq" {= "base"}
"sexp_pretty" {= "v0.15.0"}
"sexplib" {= "v0.15.1"}
"sexplib0" {= "v0.15.1"}
"spawn" {= "v0.15.1"}
"splittable_random" {= "v0.15.0"}
"stdio" {= "v0.15.0"}
"stdlib-shims" {= "0.3.0"}
"textutils" {= "v0.15.0"}
"textutils_kernel" {= "v0.15.0"}
"time_now" {= "v0.15.0"}
"timezone" {= "v0.15.0"}
"topkg" {= "1.0.5"}
"typerep" {= "v0.15.0"}
"uutf" {= "1.0.3"}
"variantslib" {= "v0.15.0"}
"zarith" {= "1.12"}
]
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
]
dev-repo: "git+https://github.com/ligolang/math-lib-cameligo.git"
4 changes: 4 additions & 0 deletions bench/bench_ocaml/bin/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(executable
(public_name bench_ocaml)
(name main)
(libraries math_lib_tests core_unix.command_unix core_bench))
21 changes: 21 additions & 0 deletions bench/bench_ocaml/bin/main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
open Core_bench

let _ =
Command_unix.run
(Bench.make_command
[
Bench.Test.create ~name:"Float lib" (fun () ->
Math_lib_tests.Test_float.all ());
Bench.Test.create ~name:"Rational lib" (fun () ->
Math_lib_tests.Test_rational.all ());
])

let _ =
Command_unix.run
(Bench.make_command
[
Bench.Test.create ~name:"Trigonomery Float lib" (fun () ->
Math_lib_tests.Test_trigo_float.all ());
Bench.Test.create ~name:"Trigonomery Rational lib" (fun () ->
Math_lib_tests.Test_trigo_rational.all ());
])
22 changes: 22 additions & 0 deletions bench/bench_ocaml/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(lang dune 3.4)

(name bench_ocaml)

(generate_opam_files true)

(source
(github ligolang/math-lib-cameligo))

(authors "ligolang")

(maintainers "Maintainer Name")

(license LICENSE)

(documentation https://url/to/documentation)

(package
(name bench_ocaml)
(depends ocaml dune core core_bench zarith))

; See the complete stanza docs at https://dune.readthedocs.io/en/stable/dune-files.html#dune-project
40 changes: 40 additions & 0 deletions bench/bench_ocaml/lib/common.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
type int = Z.t
type nat = int

let ( + ) = Z.add
let ( - ) = Z.sub
let ( * ) = Z.mul
let ( > ) = Z.gt
let ( / ) = Z.div
let ( land ) = Z.logand
let ( lsr ) = Z.shift_right
let ( mod ) = Z.rem
let two = Z.of_int 2
let three = Z.of_int 3
let four = Z.of_int 4
let five = Z.of_int 5
let six = Z.of_int 6
let seven = Z.of_int 7
let ten = Z.of_int 10
let eighteen = Z.of_int 18
let minus_one = Z.of_int (-1)
let abs x = if Z.lt x Z.zero then Z.mul minus_one x else x
let int' x = x
let assert_with_error b s = if b then () else failwith s

module Option = struct
include Option

let unopt = function Some x -> x | None -> failwith "Optinal value is none"
end

module List = struct
include List

let head_opt = function x :: _ -> Some x | _ -> None
let tail_opt = function _ :: xs -> Some xs | _ -> None
end

module Test = struct
let log = print_endline
end
3 changes: 3 additions & 0 deletions bench/bench_ocaml/lib/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(library
(name math_lib)
(libraries zarith))
94 changes: 94 additions & 0 deletions bench/bench_ocaml/lib/float.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
open Common

(* #import "math-lib-core/math.mligo" "Math" *)

(* n = a * 10^b *)
type t = { val_ : int; pow : int }

let new' (val_ : int) (pow : int) : t = { val_; pow }

let inverse (a : t) : t =
{
val_ = Z.one * Math.power (ten, eighteen) / a.val_;
pow = (a.pow * minus_one) - eighteen;
}

let add (a : t) (b : t) : t =
if a.pow < b.pow then
{
val_ = (b.val_ * Math.power (ten, abs (b.pow - a.pow))) + a.val_;
pow = a.pow;
}
else
{
val_ = (a.val_ * Math.power (ten, abs (a.pow - b.pow))) + b.val_;
pow = b.pow;
}

let sub (a : t) (b : t) : t =
if a.pow < b.pow then
{
val_ = a.val_ - (b.val_ * Math.power (ten, abs (b.pow - a.pow)));
pow = a.pow;
}
else
{
val_ = (a.val_ * Math.power (ten, abs (a.pow - b.pow))) - b.val_;
pow = b.pow;
}

let lt (a : t) (b : t) : bool =
if a.val_ < Z.zero && b.val_ > Z.zero then true
else if a.val_ > Z.zero && b.val_ < Z.zero then false
else
let diff = sub a b in
diff.val_ < Z.zero

let lte (a : t) (b : t) : bool =
if a.val_ < Z.zero && b.val_ > Z.zero then true
else if a.val_ > Z.zero && b.val_ < Z.zero then false
else
let diff = sub a b in
diff.val_ <= Z.zero

let gte (a : t) (b : t) : bool =
if a.val_ >= Z.zero && b.val_ < Z.zero then true
else if a.val_ <= Z.zero && b.val_ > Z.zero then false
else
let diff = sub a b in
diff.val_ >= Z.zero

let gt (a : t) (b : t) : bool =
if a.val_ > Z.zero && b.val_ < Z.zero then true
else if a.val_ < Z.zero && b.val_ > Z.zero then false
else
let diff = sub a b in
diff.val_ > Z.zero

let mul (a : t) (b : t) : t = { val_ = a.val_ * b.val_; pow = a.pow + b.pow }

let div (a : t) (b : t) : t =
{
val_ = a.val_ * Math.power (ten, eighteen) / b.val_;
pow = a.pow - b.pow - eighteen;
}

let modulo (a : t) (b : t) : t =
let rec compute ((a, b) : t * t) : t =
if lt a b then a else compute (sub a b, b)
in
compute (a, b)

let resolve (a : t) (prec : nat) : int =
let resolve_positif (a : t) (prec : nat) : int =
if a.pow > Z.zero then
a.val_
* Math.power (ten, abs a.pow)
* Math.power (ten, prec)
else
a.val_
* Math.power (ten, prec)
/ Math.power (ten, abs (minus_one * a.pow))
in
if a.val_ < Z.zero then minus_one * resolve_positif (new' (int' (abs a.val_)) a.pow) prec
else resolve_positif a prec
1 change: 1 addition & 0 deletions bench/bench_ocaml/lib/lib.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Math = Math
Loading