From 2385aacb1237295a4eacade285e6bc3b542ad06f Mon Sep 17 00:00:00 2001 From: Shinya Sato Date: Sat, 16 Apr 2022 20:25:55 +0900 Subject: Benchmarks for OCaml --- Changelog.md | 8 ++++++++ comparison/OCaml/Makefile | 36 ++++++++++++++++++++++++++++++++++ comparison/OCaml/ack3_11.ml | 6 ++++++ comparison/OCaml/bsort40000.ml | 23 ++++++++++++++++++++++ comparison/OCaml/fib38.ml | 7 +++++++ comparison/OCaml/isort40000.ml | 30 ++++++++++++++++++++++++++++ comparison/OCaml/msort800000.ml | 43 +++++++++++++++++++++++++++++++++++++++++ comparison/OCaml/qsort800000.ml | 30 ++++++++++++++++++++++++++++ comparison/README.txt | 2 ++ comparison/SML/fib38.sml | 4 ---- comparison/all.sh | 2 +- 11 files changed, 186 insertions(+), 5 deletions(-) create mode 100644 comparison/OCaml/Makefile create mode 100644 comparison/OCaml/ack3_11.ml create mode 100644 comparison/OCaml/bsort40000.ml create mode 100644 comparison/OCaml/fib38.ml create mode 100644 comparison/OCaml/isort40000.ml create mode 100644 comparison/OCaml/msort800000.ml create mode 100644 comparison/OCaml/qsort800000.ml create mode 100644 comparison/README.txt diff --git a/Changelog.md b/Changelog.md index 4df2d4a..3183485 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,7 +1,15 @@ # Change log +### Update on 16 April 2022 + +#### Polished + +* Several OCaml execution times have been added to the benchmark table. These programs are stored in `comparison/OCaml`. + + ### v0.8.1-1 minor update (released on 10 March 2022) + #### Polished * **Sample file (Tower of Hanoi)**: A new sample file for Tower of Hanoi is included in `sample` directory. diff --git a/comparison/OCaml/Makefile b/comparison/OCaml/Makefile new file mode 100644 index 0000000..94a4ffb --- /dev/null +++ b/comparison/OCaml/Makefile @@ -0,0 +1,36 @@ +CC = ocamlopt +CFLAGS = +BENCHMARK = bash ../tools/sptest_commands.sh +#TARGET = ack3_11 fib38 bsort40000 isort40000 qsort800000 msort800000 +TARGET = ack3_11 fib38 bsort40000 isort40000 +LOG = log.txt + + +.PHONY: all + +all: $(LOG) + +$(LOG): $(TARGET) + $(BENCHMARK) $(TARGET) + +ack3_11: ack3_11.ml + $(CC) $(CFLAGS) $< -o $@ + +fib38: fib38.ml + $(CC) $(CFLAGS) $< -o $@ + +bsort40000: bsort40000.ml + $(CC) $(CFLAGS) $< -o $@ + +isort40000: isort40000.ml + $(CC) $(CFLAGS) $< -o $@ + +qsort800000: qsort-800000.ml + $(CC) $(CFLAGS) $< -o $@ + +msort800000: msort-800000.ml + $(CC) $(CFLAGS) $< -o $@ + + +clean: + rm -f $(LOG) $(TARGET) *.o *.cmi *.cmx *~ diff --git a/comparison/OCaml/ack3_11.ml b/comparison/OCaml/ack3_11.ml new file mode 100644 index 0000000..6d4cdce --- /dev/null +++ b/comparison/OCaml/ack3_11.ml @@ -0,0 +1,6 @@ +let rec ack m n = + if m==0 then n+1 + else if n==0 then ack (m-1) 1 + else ack (m-1) (ack m (n-1)) + +let () = print_int (ack 3 11); diff --git a/comparison/OCaml/bsort40000.ml b/comparison/OCaml/bsort40000.ml new file mode 100644 index 0000000..45744bd --- /dev/null +++ b/comparison/OCaml/bsort40000.ml @@ -0,0 +1,23 @@ +let rec bs (alist : int list) n = + match alist with + (x::y::ys) -> + if x xs;; + + +let rec go xs n = + match n with + 0 -> xs + | n -> go (bs xs n) (n-1);; + +let bsort xs = go xs (List.length(xs) - 1);; + + +let rec mkRandList n = + match n with + 0 -> [] + | n -> (Random.int 10000)::(mkRandList (n-1));; + +let main = bsort (mkRandList 40000);; + diff --git a/comparison/OCaml/fib38.ml b/comparison/OCaml/fib38.ml new file mode 100644 index 0000000..2f7906d --- /dev/null +++ b/comparison/OCaml/fib38.ml @@ -0,0 +1,7 @@ +let rec fib n = + match n with + 0 -> 0 + | 1 -> 1 + | n -> fib(n-1) + fib (n-2) + +let () = print_int (fib 38); diff --git a/comparison/OCaml/isort40000.ml b/comparison/OCaml/isort40000.ml new file mode 100644 index 0000000..686a7f6 --- /dev/null +++ b/comparison/OCaml/isort40000.ml @@ -0,0 +1,30 @@ +(* +let rec insert x ylist = + match ylist with + [] -> [x] + | y::ys when x<=y -> x::y::ys + | y::ys -> y::(insert x ys);; +*) + +let rec insert x ylist = + match ylist with + [] -> [x] + | y::ys -> if x<=y + then x::y::ys + else y::(insert x ys);; + + +let rec isort (alist : int list) = + match alist with + [] -> [] + | x::xs -> insert x (isort xs);; + + + +let rec mkRandList n = + match n with + 0 -> [] + | n -> (Random.int 10000)::(mkRandList (n-1));; + +let main = isort (mkRandList 40000);; + diff --git a/comparison/OCaml/msort800000.ml b/comparison/OCaml/msort800000.ml new file mode 100644 index 0000000..1835b98 --- /dev/null +++ b/comparison/OCaml/msort800000.ml @@ -0,0 +1,43 @@ +let rec split (alist : int list) = + match alist with + [] -> ([], []) + | [a] -> ([a], []) + | a::b::cs -> + let (m,n) = split cs in + (a::m, b::m) ;; + +let rec merge (alist : int list) (blist : int list) = + match alist with + [] -> blist + | x::xs -> + match blist with + [] -> x::xs + | y::ys -> + if x [] + | a::[] -> [a] + | a::b::[] -> if a + let (m,n) = split xs + in merge (msort m) (msort n) ;; + + + +let rec mkRandList n = + match n with + 0 -> [] + | n -> (Random.int 10000)::(mkRandList (n-1)) ;; + +let main = msort (mkRandList 800000) ;; + + +(* +The following error arises: + +Stack overflow during evaluation (looping recursion?). +*) diff --git a/comparison/OCaml/qsort800000.ml b/comparison/OCaml/qsort800000.ml new file mode 100644 index 0000000..627abd2 --- /dev/null +++ b/comparison/OCaml/qsort800000.ml @@ -0,0 +1,30 @@ +let rec split pivot (alist : int list) = + match alist with + [] -> ([], []) + | (x::xs) -> + let (below, above) = split pivot xs in + if x [] + | pivot::rest -> + let (below, above) = split pivot rest + in (qsort below) @ [pivot] @ (qsort above) ;; + + + + +let rec mkRandList n = + match n with + 0 -> [] + | n -> (Random.int 10000)::(mkRandList (n-1)) ;; + +let main = qsort (mkRandList 800000) ;; + +(* +The following error arises: + +Stack overflow during evaluation (looping recursion?). +*) diff --git a/comparison/README.txt b/comparison/README.txt new file mode 100644 index 0000000..d5a1f57 --- /dev/null +++ b/comparison/README.txt @@ -0,0 +1,2 @@ +To get benchmark results, invoke the following command: +bash all.sh diff --git a/comparison/SML/fib38.sml b/comparison/SML/fib38.sml index c49ed14..504d4ac 100644 --- a/comparison/SML/fib38.sml +++ b/comparison/SML/fib38.sml @@ -2,8 +2,4 @@ fun fib 0 = 0 | fib 1 = 1 | fib n = (fib (n-1)) + (fib (n-2)); - - -(* ex *) -(* prnat(Ack (mknat 2) (mknat 3)); *) fib 38; diff --git a/comparison/all.sh b/comparison/all.sh index 424e50f..2c82c45 100644 --- a/comparison/all.sh +++ b/comparison/all.sh @@ -1,5 +1,5 @@ #!/usr/bin/bash -dir=(Haskell SML Python Inpla) +dir=(OCaml Haskell SML Python Inpla) for d in ${dir[@]} ; do # echo "cd $d; make clean; make; cd .."; # cd $d; make clean; make; cd .. -- cgit v1.2.3