From 9829fc01bc8be554a56dbc955b4c2e708ce9522c Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Fri, 8 Mar 2013 17:25:06 +0100 Subject: [PATCH] some basic benchmarks --- Makefile | 8 ++++---- bench.ml | 46 ---------------------------------------------- tests/benchs.ml | 29 +++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 50 deletions(-) delete mode 100644 bench.ml create mode 100644 tests/benchs.ml diff --git a/Makefile b/Makefile index d4d255d..b60b806 100644 --- a/Makefile +++ b/Makefile @@ -5,11 +5,11 @@ TARGETS = sequence.cma sequence.cmxa sequence.cmi sequence.a LIB = $(addprefix _build/, $(TARGETS)) INSTALL = $(LIB) sequence.mli -all: tests +all: ocamlbuild $(TARGETS) $(DOC) -bench: all - ocamlbuild -use-ocamlfind -pkg bench tests/benchs.native +benchs: all + ocamlbuild -use-ocamlfind -pkg bench -pkg unix tests/benchs.native tests: ocamlbuild -use-ocamlfind -pkg oUnit tests/run_tests.native @@ -20,4 +20,4 @@ install: all clean: ocamlbuild -clean -.PHONY: all clean tests bench +.PHONY: all clean tests benchs diff --git a/bench.ml b/bench.ml deleted file mode 100644 index 3dbef4d..0000000 --- a/bench.ml +++ /dev/null @@ -1,46 +0,0 @@ -open ExtLib - -(* sum of the ints in the list *) -let sum_list_seq l = - Sequence.fold (+) 0 (Sequence.of_list l) - -(* sum of the ints in the list *) -let sum_list_enum l = - Enum.fold (+) 0 (List.enum l) - -(* force the list *) -let force_list_seq l = - Sequence.persistent (Sequence.of_list l) - -let force_list_enum l = - let enum = List.enum l in - Enum.force enum; - enum - -let make_list n = - Sequence.to_list (Sequence.int_range ~start:0 ~stop:n) - -let measure_time f x = - let start = Unix.gettimeofday () in - let y = f x in - let stop = Unix.gettimeofday () in - y, stop -. start - -let _ = - let n = int_of_string Sys.argv.(1) in - Format.printf "compare sum on lists of size %d@." n; - let l = make_list n in - Format.printf "list created, length %d@." (List.length l); - let x1, time_seq = measure_time sum_list_seq l in - let x2, time_enum = measure_time sum_list_enum l in - assert (x1 = x2); - Format.printf "time for fold: seq: %.3f, enum: %.3f@." time_seq time_enum; - let x1, time_seq = measure_time force_list_seq l in - let x2, time_enum = measure_time force_list_enum l in - Format.printf "%d, %d@." (Sequence.length x1) (Enum.count x2); - assert (Sequence.length x1 = Enum.count x2); - Format.printf "time for force: seq: %.3f, enum: %.3f@." time_seq time_enum; - () - -(* ocamlfind ocamlopt -I `ocamlfind query sequence` -I `ocamlfind query extlib` - sequence.cmxa extLib.cmxa unix.cmxa bench.ml -o bench *) diff --git a/tests/benchs.ml b/tests/benchs.ml new file mode 100644 index 0000000..d85eca6 --- /dev/null +++ b/tests/benchs.ml @@ -0,0 +1,29 @@ + +module S = Sequence +open Sequence.Infix + +let small = [10;20;50;100;500] +let medium = small @ [1000;10_000;100_000] +let big = medium @ [500_000; 1_000_000; 2_000_000] + +let bench_fold n = + 0 -- n |> S.fold (+) 0 |> ignore + +let bench_flatmap n = + 0 -- n |> S.flatMap (fun i -> i -- (i+5)) |> (fun _ -> ()) + +let bench_product n = + S.product (0 -- n) (0 -- n) (fun (i,j) -> ()) + +let _ = + let _ = List.map + (fun (name,bench) -> + Format.printf "-------------------------------------------------------@."; + Format.printf "bench %s@." name; + bench ();) + [ "fold", (fun () -> Bench.bench_throughput bench_fold big) ; + "flatmap", (fun () -> Bench.bench_throughput bench_flatmap medium) ; + "product", (fun () -> Bench.bench_throughput bench_product small) ; + ] + in + ()