mirror of
https://github.com/c-cube/iter.git
synced 2025-12-06 03:05:29 -05:00
added a benchmark file to compare with ExtLib.Enum.t (not compiled by default,
to avoid depending on extlib)
This commit is contained in:
parent
942023cdd7
commit
907c3be9cc
2 changed files with 53 additions and 1 deletions
8
Makefile
8
Makefile
|
|
@ -1,13 +1,19 @@
|
|||
|
||||
NAME = sequence
|
||||
DOC = sequence.docdir/index.html
|
||||
TARGETS = sequence.cma sequence.cmxa sequence.cmi sequence.a
|
||||
TARGETS = sequence.cma sequence.cmxa sequence.cmi sequence.a
|
||||
LIB = $(addprefix _build/, $(TARGETS))
|
||||
INSTALL = $(LIB) sequence.mli
|
||||
|
||||
all:
|
||||
ocamlbuild $(TARGETS) $(DOC)
|
||||
|
||||
bench: all
|
||||
ocamlbuild \
|
||||
-cflags -I,`ocamlfind query extlib` \
|
||||
-lflags -I,`ocamlfind query extlib` \
|
||||
-package extlib -package unix bench.native
|
||||
|
||||
tests:
|
||||
ocamlbuild tests.native
|
||||
|
||||
|
|
|
|||
46
bench.ml
Normal file
46
bench.ml
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
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 *)
|
||||
Loading…
Add table
Reference in a new issue