From c77b579caaf1c3e37dfcab909d26a15ea8f8186c Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 7 Jun 2023 21:23:15 -0400 Subject: [PATCH] bench: add `-niter` param --- Makefile | 9 +++++---- benchs/fib_rec.ml | 30 +++++++++++++++++------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 451d30d2..d4af9c89 100644 --- a/Makefile +++ b/Makefile @@ -19,13 +19,14 @@ watch: DUNE_OPTS_BENCH?=--profile=release N?=40 +NITER?=3 bench-fib: @echo running for N=$(N) dune build $(DUNE_OPTS_BENCH) benchs/fib_rec.exe hyperfine \ - './_build/default/benchs/fib_rec.exe -psize=1 -n $(N)' \ - './_build/default/benchs/fib_rec.exe -psize=8 -n $(N)' \ - './_build/default/benchs/fib_rec.exe -psize=20 -n $(N)' \ - './_build/default/benchs/fib_rec.exe -n $(N) -seq' + './_build/default/benchs/fib_rec.exe -niter $(NITER) -psize=1 -n $(N)' \ + './_build/default/benchs/fib_rec.exe -niter $(NITER) -psize=8 -n $(N)' \ + './_build/default/benchs/fib_rec.exe -niter $(NITER) -psize=20 -n $(N)' \ + './_build/default/benchs/fib_rec.exe -niter $(NITER) -n $(N) -seq' .PHONY: test clean diff --git a/benchs/fib_rec.ml b/benchs/fib_rec.ml index f2904438..36d8c704 100644 --- a/benchs/fib_rec.ml +++ b/benchs/fib_rec.ml @@ -16,31 +16,35 @@ let rec fib ~on x : int Fut.t = let () = assert (List.init 10 fib_direct = [ 1; 1; 2; 3; 5; 8; 13; 21; 34; 55 ]) -let run ~psize ~n ~seq () : unit = - let res = - if seq then ( - Printf.printf "compute fib %d sequentially\n%!" n; - fib_direct n - ) else ( - Printf.printf "compute fib %d with pool size=%d\n%!" n psize; - let pool = Pool.create ~min:psize () in - fib ~on:pool n |> Fut.wait_block_exn - ) - in - Printf.printf "fib %d = %d\n%!" n res +let run ~psize ~n ~seq ~niter () : unit = + let pool = lazy (Pool.create ~min:psize ()) in + for _i = 1 to niter do + let res = + if seq then ( + Printf.printf "compute fib %d sequentially\n%!" n; + fib_direct n + ) else ( + Printf.printf "compute fib %d with pool size=%d\n%!" n psize; + fib ~on:(Lazy.force pool) n |> Fut.wait_block_exn + ) + in + Printf.printf "fib %d = %d\n%!" n res + done let () = let n = ref 40 in let psize = ref 16 in let seq = ref false in + let niter = ref 3 in let opts = [ "-psize", Arg.Set_int psize, " pool size"; "-n", Arg.Set_int n, " fib "; "-seq", Arg.Set seq, " sequential"; + "-niter", Arg.Set_int niter, " number of iterations"; ] |> Arg.align in Arg.parse opts ignore ""; - run ~psize:!psize ~n:!n ~seq:!seq () + run ~psize:!psize ~n:!n ~seq:!seq ~niter:!niter ()