A micro-benchmark to compare modulo, arbitrary-precised, and saturation arithmetics
The snippet can be accessed without any authentication.
Authored by
Yann Regis-Gianas
Edited
bench.ml 2.33 KiB
let repeat n f =
let rec aux accu k =
if k = 0 then accu else aux (f () :: accu) (k - 1)
in
aux [] n
open! Core
open Core_bench
let rec random () =
match Int64.to_int (Random.int64 (Int64.of_int Int.max_value)) with
| None -> random ()
| Some x -> x
let inputs =
Array.of_list (repeat 10000 (fun () -> (random (), random ())))
let zinputs =
Array.map ~f:(fun (x, y) -> (Z.of_int x, Z.of_int y)) inputs
let rec compose k op x y =
if k = 0 then
op x y
else
compose (k - 1) op (op x y) y
let test op = Array.map ~f:(fun (x, y) -> compose 10 op x y)
let ztest op = test op zinputs
let itest op = test op inputs
let zarith_add_test () =
ignore (Array.length (ztest Z.add))
let native_add_test () =
ignore (Array.length (itest Stdlib.( + )))
let saturation_add_test () =
itest Saturation_repr.add
let zarith_sub_test () =
ignore (Array.length (ztest Z.sub))
let native_sub_test () =
ignore (Array.length (itest Stdlib.( - )))
let saturation_sub_test () =
itest Saturation_repr.sub
let zarith_mul_test () =
ignore (Array.length (ztest Z.mul))
let native_mul_test () =
ignore (Array.length (itest Stdlib.( * )))
let saturation_mul_test () =
itest Saturation_repr.mul
let zarith_div_test () =
ignore (Array.length (ztest Z.div))
let native_div_test () =
ignore (Array.length (itest Stdlib.( / )))
let saturation_div_test () =
itest Saturation_repr.ediv
let () =
Random.self_init ();
Command.run (Bench.make_command [
Bench.Test.create ~name:"Zarith add" zarith_add_test;
Bench.Test.create ~name:"Native add" native_add_test;
Bench.Test.create ~name:"Saturation add" saturation_add_test
]);
Command.run (Bench.make_command [
Bench.Test.create ~name:"Zarith sub" zarith_sub_test;
Bench.Test.create ~name:"Native sub" native_sub_test;
Bench.Test.create ~name:"Saturation sub" saturation_sub_test;
]);
Command.run (Bench.make_command [
Bench.Test.create ~name:"Zarith mul" zarith_mul_test;
Bench.Test.create ~name:"Native mul" native_mul_test;
Bench.Test.create ~name:"Saturation mul" saturation_mul_test;
]);
Command.run (Bench.make_command [
Bench.Test.create ~name:"Zarith div" zarith_div_test;
Bench.Test.create ~name:"Native div" native_div_test;
Bench.Test.create ~name:"Saturation div" saturation_div_test;
])
Please register or sign in to comment