some basic benchmarks

This commit is contained in:
Simon Cruanes 2013-03-08 17:25:06 +01:00
parent cb113e5174
commit 9829fc01bc
3 changed files with 33 additions and 50 deletions

View file

@ -5,11 +5,11 @@ TARGETS = sequence.cma sequence.cmxa sequence.cmi sequence.a
LIB = $(addprefix _build/, $(TARGETS)) LIB = $(addprefix _build/, $(TARGETS))
INSTALL = $(LIB) sequence.mli INSTALL = $(LIB) sequence.mli
all: tests all:
ocamlbuild $(TARGETS) $(DOC) ocamlbuild $(TARGETS) $(DOC)
bench: all benchs: all
ocamlbuild -use-ocamlfind -pkg bench tests/benchs.native ocamlbuild -use-ocamlfind -pkg bench -pkg unix tests/benchs.native
tests: tests:
ocamlbuild -use-ocamlfind -pkg oUnit tests/run_tests.native ocamlbuild -use-ocamlfind -pkg oUnit tests/run_tests.native
@ -20,4 +20,4 @@ install: all
clean: clean:
ocamlbuild -clean ocamlbuild -clean
.PHONY: all clean tests bench .PHONY: all clean tests benchs

View file

@ -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 *)

29
tests/benchs.ml Normal file
View file

@ -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
()