diff options
| author | Shinya Sato <shinnya.sato@gmail.com> | 2021-10-24 23:04:45 +0900 |
|---|---|---|
| committer | Shinya Sato <shinnya.sato@gmail.com> | 2021-10-24 23:04:45 +0900 |
| commit | 787dc13c837f0955fbd85b8f26a05d720d361333 (patch) | |
| tree | fec3abebb1e867e8b2239d7cf07b4c7d13ce0f81 /comparison | |
| parent | e607a57dfafb784c4e27da143214afb2ccd07fbf (diff) | |
| download | inpla-787dc13c837f0955fbd85b8f26a05d720d361333.tar.gz inpla-787dc13c837f0955fbd85b8f26a05d720d361333.zip | |
Upload the comparison directory
Diffstat (limited to '')
42 files changed, 1333 insertions, 0 deletions
diff --git a/comparison/Haskell/Makefile b/comparison/Haskell/Makefile new file mode 100644 index 0000000..0042c28 --- /dev/null +++ b/comparison/Haskell/Makefile @@ -0,0 +1,36 @@ +CC = stack ghc +CFLAGS = -O2 +BENCHMARK = bash ../tools/sptest_commands.sh +TARGET = ack3-11 fib38 bsort-40000 isort-40000 qsort-800000 msort-800000 +#TARGET = ack3-11 +LOG = log.txt + + +.PHONY: all + +all: $(LOG) + +$(LOG): $(TARGET) + $(BENCHMARK) $(TARGET) + +ack3-11: ack3-11.hs + $(CC) -- $(CFLAGS) $< + +fib38: fib38.hs + $(CC) -- $(CFLAGS) $< + +bsort-40000: bsort-40000.hs + $(CC) -- $(CFLAGS) $< + +isort-40000: isort-40000.hs + $(CC) -- $(CFLAGS) $< + +qsort-800000: qsort-800000.hs + $(CC) -- $(CFLAGS) $< + +msort-800000: msort-800000.hs + $(CC) -- $(CFLAGS) $< + + +clean: + rm -f $(LOG) $(TARGET) *.o *.hi *~ diff --git a/comparison/Haskell/ack3-11.hs b/comparison/Haskell/ack3-11.hs new file mode 100644 index 0000000..7c8f350 --- /dev/null +++ b/comparison/Haskell/ack3-11.hs @@ -0,0 +1,9 @@ +ack 0 n = n+1 +ack m 0 = ack (m-1) 1 +ack m n = ack (m-1) (ack m (n-1)) + + +main = print $ (ack 3 11) + + + diff --git a/comparison/Haskell/bsort-40000.hs b/comparison/Haskell/bsort-40000.hs new file mode 100644 index 0000000..1940917 --- /dev/null +++ b/comparison/Haskell/bsort-40000.hs @@ -0,0 +1,38 @@ +{- +https://gist.github.com/masquerade0324/68f4f3fd3ab897fbe1ad +http://files.farka.eu/pub/AC21007/lec5.pdf +-} + +import System.Random + +bs xs 0 = xs +bs (x:y:ys) n = + if x<y then x:(bs (y:ys) (n-1)) + else y:(bs (x:ys) (n-1)) +bs xs n = xs + + +go xs 0 = xs +go xs n = go (bs xs n) (n-1) + +bsort xs = go xs (length xs -1) + +{- +main = print $ bsort [3,2,8,5] +-} + + + +randomList :: Int -> IO([Int]) +randomList 0 = return [] +randomList n = do + r <- randomRIO (1,10000) + rs <- randomList (n-1) + return (r:rs) + + + +main = do + list <- randomList 40000 + let sorted = bsort list + print (take 10 sorted) diff --git a/comparison/Haskell/fib38.hs b/comparison/Haskell/fib38.hs new file mode 100644 index 0000000..96040eb --- /dev/null +++ b/comparison/Haskell/fib38.hs @@ -0,0 +1,9 @@ +fib 0 = 0 +fib 1 = 1 +fib n = fib (n - 2) + fib (n - 1) + + +main = print $ fib 38 + + + diff --git a/comparison/Haskell/isort-40000.hs b/comparison/Haskell/isort-40000.hs new file mode 100644 index 0000000..f834d00 --- /dev/null +++ b/comparison/Haskell/isort-40000.hs @@ -0,0 +1,36 @@ +{- +(* http://www.codecodex.com/wiki/Insertion_sort#Standard_ML *) +-} +import System.Random + +isort [] = [] +isort (x:xs) = insert x (isort xs) + +insert x [] = [x] +insert x (y:ys) = + if x<=y then x:y:ys + else y:(insert x ys) + + +{- +main = print $ isort [3,2,8,5] +-} + + +randomList :: Int -> IO([Int]) +randomList 0 = return [] +randomList n = do + r <- randomRIO (1,10000) + rs <- randomList (n-1) + return (r:rs) + + + +main = do + list <- randomList 40000 + let sorted = isort list + print (take 10 sorted) + + + + diff --git a/comparison/Haskell/msort-800000.hs b/comparison/Haskell/msort-800000.hs new file mode 100644 index 0000000..eb1a24d --- /dev/null +++ b/comparison/Haskell/msort-800000.hs @@ -0,0 +1,45 @@ +{- +https://stackoverflow.com/questions/25839939/understanding-merge-sort-in-ml +-} +import System.Random + +mysplit [] = ([], []) +mysplit [a] = ([a], []) +mysplit (a:b:cs) = (a:m, b:n) + where + (m,n) = mysplit cs + +merge ([], ys) = ys +merge (xs, []) = xs +merge (x:xs, y:ys) + | x<y = x:merge(xs, y:ys) + | otherwise = y:merge(x:xs, ys) + +msort [] = [] +msort [a] = [a] +msort [a,b] + | a<b = [a,b] + | otherwise = [b,a] +msort xs = merge (msort m, msort n) + where + (m,n) = mysplit xs + +{- +main = print $ msort [3,2,8,5] +-} + +randomList :: Int -> IO([Int]) +randomList 0 = return [] +randomList n = do + r <- randomRIO (1,10000) + rs <- randomList (n-1) + return (r:rs) + + + +main = do + list <- randomList 800000 + let sorted = msort list + print (take 10 sorted) + + diff --git a/comparison/Haskell/qsort-800000.hs b/comparison/Haskell/qsort-800000.hs new file mode 100644 index 0000000..492efef --- /dev/null +++ b/comparison/Haskell/qsort-800000.hs @@ -0,0 +1,37 @@ +{- +(* http://www.codecodex.com/wiki/Insertion_sort#Standard_ML *) +-} +import System.Random + + +qsort [] = [] +qsort [x] = [x] +qsort (x:xs) = (qsort left) ++ [x] ++ (qsort right) + where (left, right) = part x xs + +part pivot [] = ([], []) +part pivot (x:xs) + | x<pivot = (x:below, above) + | otherwise = (below, x:above) + where + (below, above) = part pivot xs + + +{- +main = print $ qsort [3,2,8,5] +-} + +randomList :: Int -> IO([Int]) +randomList 0 = return [] +randomList n = do + r <- randomRIO (1,10000) + rs <- randomList (n-1) + return (r:rs) + + + +main = do + list <- randomList 800000 + let sorted = qsort list + print (take 10 sorted) + diff --git a/comparison/Inpla/Makefile b/comparison/Inpla/Makefile new file mode 100644 index 0000000..8ca3630 --- /dev/null +++ b/comparison/Inpla/Makefile @@ -0,0 +1,8 @@ +.PHONY: all + + +all: + sh main.sh + +clean: + rm -f ./inplaSeq ./inplaPara log-* *~ diff --git a/comparison/Inpla/main.sh b/comparison/Inpla/main.sh new file mode 100644 index 0000000..b0da0e2 --- /dev/null +++ b/comparison/Inpla/main.sh @@ -0,0 +1,16 @@ +#!/bin/sh + +./sptestInpla.sh src/fib-38.in +./sptestInpla.sh src/fib-38-reuse.in + +./sptestInpla.sh src/ack-stream_3-11.in +./sptestInpla.sh src/ack-stream_3-11-reuse.in + +./sptestInpla.sh src/bsort-40000.in '-c 200000' +./sptestInpla.sh src/bsort-40000-reuse.in '-c 200000' + +./sptestInpla.sh src/qsort-800000.in '-c 8000000' +./sptestInpla.sh src/qsort-800000-reuse.in '-c 8000000' + +./sptestInpla.sh src/msort-800000.in '-c 8000000' +./sptestInpla.sh src/msort-800000-reuse.in '-c 8000000' diff --git a/comparison/Inpla/sptestInpla.sh b/comparison/Inpla/sptestInpla.sh new file mode 100644 index 0000000..36044a6 --- /dev/null +++ b/comparison/Inpla/sptestInpla.sh @@ -0,0 +1,139 @@ +#!/bin/bash +# $1: the source file name +# $2: options +# example: ./sptestInpla.sh A3_10.in '-x 100000' + + +# repeat and waittime +ct=1 +waittime=5 + +max_threads=9 + +# options +source=$1 +inpla_seq='inplaSeq' +inpla_para='inplaPara' +opt=$2 +#dir_source='prog/' +dir_make='../../' +path_source="$dir_source$source" +fname=`echo $1 | sed -e s'|.*/\(.*\)|\1|'` + +log="log-${fname}.txt" + + +# main ------------------------------------------------------ +if [ -z $1 ]; then + echo "Usage: ./sptestInpla.sh filename option" + exit +fi + +if [ ! -e $path_source ]; then + echo "$path_source is not found." + exit +fi + +if [ ! -e $inpla_seq -a ! -e "${inpla_seq}.exe" ]; then + echo "Now making Inpla in the non-threaded version as '$inpla_seq'." + make -C "$dir_make" clean + make -C "$dir_make" +if [ -e 'inpla.exe' ]; then + inpla_seq="${inpla_seq}.exe" + mv ../../inpla.exe ./$inpla_seq +else + mv ../../inpla ./$inpla_seq +fi +fi + +if [ ! -e $inpla_para -a ! -e "${inpla_para}.exe" ]; then + echo "Now making Inpla in the multi-threaded version as '$inpla_para'." + make -C "$dir_make" clean + make -C "$dir_make" thread +if [ -e 'inpla.exe' ]; then + inpla_para="${inpla_para}.exe" + mv ../../inpla.exe ./$inplla_para +else + mv ../../inpla ./$inpla_para +fi +fi + + +# ------------------------------------------------- +function mywait() { + for w in $(seq 1 $waittime) ; do + echo -n "." + sleep 1 + done +} + +function gettime() { + # $1: the execution command + rm -f _result.txt + for i in $(seq 1 $ct) ; do + (time $1) 2>> _result.txt + echo + echo -n "Done ($i/$ct)" + if [[ $(($i + 1)) -le $ct ]]; then + mywait + echo 'Restart' + fi + done + echo + echo + logtime=`grep real _result.txt | cut -f2 | sed -e "s/\(.*\)m\(.*\)s/\1 \2/" | awk -F' ' '{print ($1*60+$2)}'` + echo "Execution time" + echo "$logtime" + + # for return value + res=`echo "$logtime\n" | awk 'BEGIN{r=0;c=0} {r=r+$0;c++} END{print r/c}'` + rm -f _result.txt +} + +function puts_header() { + # $1: the name of the log file + # $2: a program name + # ex: puts_header $log + + local alog + alog=$1 + + echo '' >> $alog + echo "$2,$ct" | awk -F',' '{printf("%-27s| avg. time over %d runs (sec)\n",$1,$2)}' >> $alog + echo '===========================+=============================' >> $alog +} + +function do_experiment() { + # $1: execution command + # $2: the header string to put the logfile + # ex: do_experiment "$inpla -f $source $opt_special $opt" + echo -n 'Cooling CPU' + mywait + echo + echo "------------------" + echo "$1" + gettime "$1" + echo "Average ${res}sec" + echo "$2,$res" | awk -F',' '{printf("%-27s| %.2f\n",$1,$2)}' >> $log +} + + + +# clear the previous log +rm -f $log + + +puts_header $log "$source $opt" + +# Execution in sequential +do_experiment "./$inpla_seq -f $path_source $opt" "non-thread" + +# Execution in parallel +for ((thread=1; thread<=max_threads; thread++)) +do + do_experiment "./$inpla_para -f $path_source $opt -t $thread" "-t $thread" +done + +echo +cat $log + diff --git a/comparison/Inpla/src/ack-stream_3-11-reuse.in b/comparison/Inpla/src/ack-stream_3-11-reuse.in new file mode 100644 index 0000000..7d7c701 --- /dev/null +++ b/comparison/Inpla/src/ack-stream_3-11-reuse.in @@ -0,0 +1,42 @@ +// Ackermann Function: streaming version +// +// fun ack(0,n) = n+1 +// | ack(m,0) = ack(m-1,1) +// | ack(m,n) = ack(m-1,ack(m,n-1)); + +// The below rule is wrong because there is no gurantee +// such that cnt is an integer agent when Ack(r)~(m-1,cnt) is evaluated. +// +// Ack(r) >< (int m, int n) +// | m==0 => r~(n+1) +// | n==0 => Ack(r) ~ (m-1,1) +// | _ => Ack(cnt)~(m,n-1), Ack(r)~(m-1,cnt); + + +// When m==0, we should not have OneUp(ret)~n +// because the n is correspoidng to cnt in Ack(ret,cnt)~(m-1), +// and we have to wait for finishing evaluation of Ack(cnt,n-1)~m. +// So, without waiting, we put ret~Inc(n) in order to continue evaluation. +// The the number of Inc is evaluated later by using Sumup. + +Ack(ret, n) >< (int m) +| m==0 => ret~(*L)Inc(n) +| _ => (*L)Ackm(ret, m)~n; + +Ackm(ret, int m) >< (int n) +| n==0 => Ack(ret,Inc(0)) ~ (m-1) +| _ => (*L)Ack(cnt,n-1)~m, Ack(ret,cnt)~(m-1); + +Ackm(ret, int m) >< Inc(n) +=> (*L)Ack(cnt,n)~m, (*R)Ack(ret,cnt)~(m-1); + +Sumup(ret, int sum) >< Inc(x) => (*L)Sumup(ret, sum+1)~x; +Sumup(ret, int sum) >< (int n) => ret~sum+n; + + +//AckMain(ret)><(int m, int n) => (*L)Ack(cnt,n)~m, (*R)Sumup(ret,0)~cnt; +AckMain(ret)><(int m, int n) => Ack(cnt,n)~m, Sumup(ret,0)~cnt; + +AckMain(ret)~(3,11); +ret; +free ret; diff --git a/comparison/Inpla/src/ack-stream_3-11.in b/comparison/Inpla/src/ack-stream_3-11.in new file mode 100644 index 0000000..7b23d32 --- /dev/null +++ b/comparison/Inpla/src/ack-stream_3-11.in @@ -0,0 +1,41 @@ +// Ackermann Function: streaming version +// +// fun ack(0,n) = n+1 +// | ack(m,0) = ack(m-1,1) +// | ack(m,n) = ack(m-1,ack(m,n-1)); + +// The below rule is wrong because there is no gurantee +// such that cnt is an integer agent when Ack(r)~(m-1,cnt) is evaluated. +// +// Ack(r) >< (int m, int n) +// | m==0 => r~(n+1) +// | n==0 => Ack(r) ~ (m-1,1) +// | _ => Ack(cnt)~(m,n-1), Ack(r)~(m-1,cnt); + + +// When m==0, we should not have OneUp(ret)~n +// because the n is correspoidng to cnt in Ack(ret,cnt)~(m-1), +// and we have to wait for finishing evaluation of Ack(cnt,n-1)~m. +// So, without waiting, we put ret~Inc(n) in order to continue evaluation. +// The the number of Inc is evaluated later by using Sumup. + +Ack(ret, n) >< (int m) +| m==0 => ret~Inc(n) +| _ => Ackm(ret, m)~n; + +Ackm(ret, int m) >< (int n) +| n==0 => Ack(ret,Inc(0)) ~ (m-1) +| _ => Ack(cnt,n-1)~m, Ack(ret,cnt)~(m-1); + +Ackm(ret, int m) >< Inc(n) +=> Ack(cnt,n)~m, Ack(ret,cnt)~(m-1); + +Sumup(ret, int sum) >< Inc(x) => Sumup(ret, sum+1)~x; +Sumup(ret, int sum) >< (int n) => ret~sum+n; + + +AckMain(ret)><(int m, int n) => Ack(cnt,n)~m, Sumup(ret,0)~cnt; + +AckMain(ret)~(3,11); +ret; +free ret; diff --git a/comparison/Inpla/src/bsort-40000-reuse.in b/comparison/Inpla/src/bsort-40000-reuse.in new file mode 100644 index 0000000..8b6a386 --- /dev/null +++ b/comparison/Inpla/src/bsort-40000-reuse.in @@ -0,0 +1,24 @@ +// Bubble sort +BS(ret) >< [] => ret~[]; +BS(ret) >< x:xs => (*L)BS(ret)~cnt, (*R)B(cnt, x)~xs; +BS(ret) >< M(x) => ret~x; + +B(ret, int x) >< [] => ret~M([x]); +B(ret, int x) >< M(w) => ret~M(x:w); +B(ret, int x) >< (int y):ys +| x<y => ret~(*R)(x:cnt), (*L)B(cnt, y)~ys +| _ => ret~(*R)(y:cnt), (*L)B(cnt, x)~ys; + + + + +const LIST_ELEM=40000; + +MkRandList(ret) >< (int n) +| n>0 => ret~(rd:cnt), (*L)MkRandList(cnt)~(n-1) + where rd=rand(10000) +| _ => ret~[]; + +MkRandList(cnt)~(LIST_ELEM), +BS(ret)~cnt; +ret; diff --git a/comparison/Inpla/src/bsort-40000.in b/comparison/Inpla/src/bsort-40000.in new file mode 100644 index 0000000..0120900 --- /dev/null +++ b/comparison/Inpla/src/bsort-40000.in @@ -0,0 +1,24 @@ +// Bubble sort +BS(ret) >< [] => ret~[]; +BS(ret) >< x:xs => BS(ret)~cnt, B(cnt, x)~xs; +BS(ret) >< M(x) => ret~x; + +B(ret, int x) >< [] => ret~M([x]); +B(ret, int x) >< M(w) => ret~M(x:w); +B(ret, int x) >< (int y):ys +| x<y => ret~(x:cnt), B(cnt, y)~ys +| _ => ret~(y:cnt), B(cnt, x)~ys; + + + + +const LIST_ELEM=40000; + +MkRandList(ret) >< (int n) +| n>0 => ret~(rd:cnt), MkRandList(cnt)~(n-1) + where rd=rand(10000) +| _ => ret~[]; + +MkRandList(cnt)~(LIST_ELEM), +BS(ret)~cnt; +ret; diff --git a/comparison/Inpla/src/fib-38-reuse.in b/comparison/Inpla/src/fib-38-reuse.in new file mode 100644 index 0000000..6b3d32b --- /dev/null +++ b/comparison/Inpla/src/fib-38-reuse.in @@ -0,0 +1,17 @@ +// Fibonacci number +Fib(r) >< (int n) +| n == 0 => r~0 +| n == 1 => r~1 +| _ => Fib(cnt1)~(n-1), Fib(cnt2)~(n-2), (*L)Add(r,cnt2)~cnt1; + +/* Add is realised as built-in. +Addd(r,n) >< (int m) +=> Addd2(r,m) ~ n; + +Addd2(r,int m) >< (int n) +=> r~(m+n); +*/ + +Fib(r)~38; +r; +free r; diff --git a/comparison/Inpla/src/fib-38.in b/comparison/Inpla/src/fib-38.in new file mode 100644 index 0000000..a79eb5b --- /dev/null +++ b/comparison/Inpla/src/fib-38.in @@ -0,0 +1,17 @@ +// Fibonacci number +Fib(r) >< (int n) +| n == 0 => r~0 +| n == 1 => r~1 +| _ => Add(r,cnt2)~cnt1, Fib(cnt1)~(n-1), Fib(cnt2)~(n-2); + +/* +Addd(r,n) >< (int m) +=> Addd2(r,m) ~ n; + +Addd2(r,int m) >< (int n) +=> r~(m+n); +*/ + +Fib(r)~38; +r; +free r; diff --git a/comparison/Inpla/src/isort-40000-reuse.in b/comparison/Inpla/src/isort-40000-reuse.in new file mode 100644 index 0000000..00f0ecc --- /dev/null +++ b/comparison/Inpla/src/isort-40000-reuse.in @@ -0,0 +1,22 @@ +// Insertion sort +IS(ret) >< [] => ret~[]; +IS(ret) >< x:xs => (*L)I(ret, x)~cnt, (*R)IS(cnt)~xs; + +I(ret, int x) >< [] => ret~[x]; +I(ret, int x) >< (int y):ys +| x<=y => ret~(*L)(x:(*R)(y:ys)) +| _ => ret~(*L)(y:cnt), (*R)I(cnt, x)~ys; + + +const LIST_ELEM=40000; + +MkRandList(ret) >< (int n) +| n>0 => ret~(rd:cnt), (*L)MkRandList(cnt)~(n-1) + where rd=rand(10000) +| _ => ret~[]; + +MkRandList(cnt)~(LIST_ELEM), +IS(ret)~cnt; +ret; + + diff --git a/comparison/Inpla/src/isort-40000.in b/comparison/Inpla/src/isort-40000.in new file mode 100644 index 0000000..ff831a3 --- /dev/null +++ b/comparison/Inpla/src/isort-40000.in @@ -0,0 +1,22 @@ +// Insertion sort +IS(ret) >< [] => ret~[]; +IS(ret) >< x:xs => I(ret, x)~cnt, IS(cnt)~xs; + +I(ret, int x) >< [] => ret~[x]; +I(ret, int x) >< (int y):ys +| x<=y => ret~(x:y:ys) +| _ => ret~(y:cnt), I(cnt, x)~ys; + + +const LIST_ELEM=40000; + +MkRandList(ret) >< (int n) +| n>0 => ret~(rd:cnt), MkRandList(cnt)~(n-1) + where rd=rand(10000) +| _ => ret~[]; + +MkRandList(cnt)~(LIST_ELEM), +IS(ret)~cnt; +ret; + + diff --git a/comparison/Inpla/src/msort-800000-reuse.in b/comparison/Inpla/src/msort-800000-reuse.in new file mode 100644 index 0000000..36723b9 --- /dev/null +++ b/comparison/Inpla/src/msort-800000-reuse.in @@ -0,0 +1,67 @@ +// Merge sort +MS(ret) >< [] => ret~[]; +MS(ret) >< x:xs => MS_tail(ret, x)~xs; + +MS_tail(ret, n) >< [] => ret~(*R)[n]; +MS_tail(ret, int n) >< x:xs => +// MS(a)~left, MS(b)~right, Append(ret,a)~b, +// Split(0, left,right) ~ (n:x:xs); + MS(a)~left, MS(b)~right, Append(ret,a)~b, + (*L)Split(left,right) ~ (*R)(n:x:xs); + + +/* +Splitn(int flag, a, b) >< [] => a~[], b~[]; +Splitn(int flag, a, b) >< (int y):ys +| flag==0 => b~(*R)(y:cnt), (*L)Splitn(1, a, cnt)~ys +| _ => a~(*R)(y:cnt), (*L)Splitn(0, cnt, b)~ys; + +Split(a,b) >< x:xs => Splitn(0,a,b)~x:xs; +*/ + +Split(a,b) >< [] => a~[], b~[]; +Split(a,b) >< x:xs => a~(x:cntl), (*L)Split(b,cntl)~xs; + + + +// この実験から、Merge は悪くないことがわかる。 +// Part でも -t 5 6 7 順に速くなる。 +// つまり、Split が悪いと言える。 + +/* +MS_tail(ret, n) >< [] => ret~(*R)[n]; +MS_tail(ret, n) >< x:xs => + MS(a)~left, MS(b)~right, Merge(ret,b)~a, + (*L)Split(left,right) ~ (*R)(n:x:xs); +*/ + +Merge(ret, snd) >< [] => ret~snd; +Merge(ret, snd) >< x:xs => (*L)MergeCC(ret, x, xs)~snd; + +MergeCC(ret, int y, ys) >< [] => ret~(*R)(y:ys); +MergeCC(ret, int y, ys) >< (int x):xs +| x <= y => ret~(x:cnt), (*L)MergeCC(cnt, y, ys) ~ xs +| _ => ret~(y:cnt), (*L)MergeCC(cnt, x, xs) ~ ys; +// ret~(*R)(x:cnt) less improvement + +// *** I should make an option that outputs bytecodes to a file. *** +// *** Then, I have to look the reason why +// *** ret~(*R)(x:cnt), MergeCC(cnt, y, ys) ~ xs becomes less improvement. + + + + +// left~cntr, (*L)Split(cntr,cntl)~xs, right~(x:cntl); +// This also make it less efficient. + + +const LIST_ELEM=1200000; + +MkRandList(ret) >< (int n) +| n>0 => ret~(rd:cnt), (*L)MkRandList(cnt)~(n-1) + where rd=rand(LIST_ELEM) +| _ => ret~[]; + +MkRandList(cnt)~(LIST_ELEM), +MS(ret)~cnt; +ret; diff --git a/comparison/Inpla/src/msort-800000.in b/comparison/Inpla/src/msort-800000.in new file mode 100644 index 0000000..e04803f --- /dev/null +++ b/comparison/Inpla/src/msort-800000.in @@ -0,0 +1,34 @@ +// Merge sort +MS(ret) >< [] => ret~[]; +MS(ret) >< x:xs => MS_tail(ret, x)~xs; + +MS_tail(ret, int n) >< [] => ret~[n]; +MS_tail(ret, int n) >< x:xs => + Split(left,right) ~ (n:x:xs), + MS(a)~left, MS(b)~right, Merge(ret,b)~a; + +Merge(ret, snd) >< [] => ret~snd; +Merge(ret, snd) >< x:xs => MergeC(ret, x, xs)~snd; + +MergeCC(ret, int y, ys) >< [] => ret~(y:ys); +MergeCC(ret, int y, ys) >< (int x):xs +| x <= y => ret~(x:cnt), MergeCC(cnt, y, ys) ~ xs +| _ => ret~(y:cnt), MergeCC(cnt, x, xs) ~ ys; + + +Split(right,left) >< [] => right~[], left~[]; +Split(right,left) >< x:xs => + right~(x:cntl), left~cntr, Split(cntr,cntl)~xs; + + + +const LIST_ELEM=800000; + +MkRandList(ret) >< (int n) +| n>0 => ret~(rd:cnt), MkRandList(cnt)~(n-1) + where rd=rand(10000) +| _ => ret~[]; + +MkRandList(cnt)~(LIST_ELEM), +MS(ret)~cnt; +ret; diff --git a/comparison/Inpla/src/qsort-800000-reuse.in b/comparison/Inpla/src/qsort-800000-reuse.in new file mode 100644 index 0000000..8bf7a83 --- /dev/null +++ b/comparison/Inpla/src/qsort-800000-reuse.in @@ -0,0 +1,29 @@ +// Quick sort +QS(ret) >< [] => ret~(*R)[]; +QS(ret) >< (int x):xs => + (*R)Part(x, left, right)~xs, + (*L)QS(cntl)~left, QS(cntr)~right, + Append(ret, x:cntl)~cntr; + +// Note: `Append' is implemented as built-in. + +// Apnd(ret, a)~b --> ret ~ b@a +//Apnd(ret, a) >< [] => ret~a; +//Apnd(ret, a) >< x:xs => ret~(x:cnt), Apnd(cnt, a)~xs; + +Part(int x, a, b) >< [] => a~(*L)[], b~(*R)[]; +Part(int x, a, b) >< (int y):ys +| y<x => b~(*R)(y:cnt), (*L)Part(x, a, cnt)~ys +| _ => a~(*R)(y:cnt), (*L)Part(x, cnt, b)~ys; + + +const LIST_ELEM=800000; + +MkRandList(ret) >< (int n) +| n>0 => ret~(rd:cnt), (*L)MkRandList(cnt)~(n-1) + where rd=rand(10000) +| _ => ret~[]; + +MkRandList(cnt)~(LIST_ELEM), +QS(ret)~cnt; +ret; diff --git a/comparison/Inpla/src/qsort-800000.in b/comparison/Inpla/src/qsort-800000.in new file mode 100644 index 0000000..e8913e8 --- /dev/null +++ b/comparison/Inpla/src/qsort-800000.in @@ -0,0 +1,29 @@ +// Quick sort +QS(ret) >< [] => ret~[]; +QS(ret) >< (int x):xs => + Part(x, left, right)~xs, + QS(cntl)~left, QS(cntr)~right, + Append(ret, x:cntl)~cntr; + +// Note: `Append' is implemented as built-in. + +// Apnd(ret, a)~b --> ret ~ b@a +//Apnd(ret, a) >< [] => ret~a; +//Apnd(ret, a) >< x:xs => ret~(x:cnt), Apnd(cnt, a)~xs; + +Part(int x, a, b) >< [] => a~[], b~[]; +Part(int x, a, b) >< (int y):ys +| y<x => b~(y:cnt), Part(x, a, cnt)~ys +| _ => a~(y:cnt), Part(x, cnt, b)~ys; + + +const LIST_ELEM=800000; + +MkRandList(ret) >< (int n) +| n>0 => ret~(rd:cnt), MkRandList(cnt)~(n-1) + where rd=rand(10000) +| _ => ret~[]; + +MkRandList(cnt)~(LIST_ELEM), +QS(ret)~cnt; +ret; diff --git a/comparison/Python/Makefile b/comparison/Python/Makefile new file mode 100644 index 0000000..fb6de82 --- /dev/null +++ b/comparison/Python/Makefile @@ -0,0 +1,16 @@ +COMMAND = python3 +#TARGET = ack3-11.py fib38.py bsort-40000.py isort-40000.py qsort-800000.py msort-800000.py +TARGET = fib38.py bsort-40000.py isort-40000.py qsort-800000.py msort-800000.py +BENCHMARK = bash ../tools/sptest_scripts.sh +LOG = log.txt + + +.PHONY: all + +all: $(LOG) + +$(LOG): $(TARGET) + $(BENCHMARK) $(COMMAND) '$(TARGET)' + +clean: + rm -f $(LOG) *~ diff --git a/comparison/Python/ack3-11.py b/comparison/Python/ack3-11.py new file mode 100644 index 0000000..3e831ac --- /dev/null +++ b/comparison/Python/ack3-11.py @@ -0,0 +1,9 @@ +def ack(m,n): + if m==0: + return n+1 + elif n==0: + return ack(m-1, 1) + else: + return ack(m-1, ack(m, n-1)) + +print(ack(3,11)) diff --git a/comparison/Python/bsort-40000.py b/comparison/Python/bsort-40000.py new file mode 100644 index 0000000..25ea4c7 --- /dev/null +++ b/comparison/Python/bsort-40000.py @@ -0,0 +1,24 @@ +# http://www.geekviewpoint.com/python/sorting/bubblesort +import random + +def bubblesort(A): + for i in range(len(A)): + for k in range(len(A)-1, i, -1): + if (A[k] < A[k-1]): + tmp = A[k] + A[k] = A[k-1] + A[k-1] = tmp + + +def mkRandList(n): + a=[] + for i in range(1,n+1): + a.insert(0, random.randint(0,10000)) + return a + + + +a=mkRandList(40000) +bubblesort(a) + +#print(a) diff --git a/comparison/Python/fib38.py b/comparison/Python/fib38.py new file mode 100644 index 0000000..efa5380 --- /dev/null +++ b/comparison/Python/fib38.py @@ -0,0 +1,8 @@ +def fib(n): + if n == 0: + return 0 + elif n == 1: + return 1 + else: + return fib(n-1) + fib(n-2) +print(fib(38)) diff --git a/comparison/Python/isort-40000.py b/comparison/Python/isort-40000.py new file mode 100644 index 0000000..6fbadb7 --- /dev/null +++ b/comparison/Python/isort-40000.py @@ -0,0 +1,24 @@ +import random + +def insert_sort(arr): + for i in range(1, len(arr)): + j = i - 1 + ele = arr[i] + while arr[j] > ele and j >= 0: + arr[j + 1] = arr[j] + j -= 1 + arr[j + 1] = ele + return arr + + +def mkRandList(n): + a=[] + for i in range(1,n+1): + a.insert(0, random.randint(0,10000)) + return a + +a = mkRandList(40000) +insert_sort(a) +#print(a) + + diff --git a/comparison/Python/msort-800000.py b/comparison/Python/msort-800000.py new file mode 100644 index 0000000..f8b8bd1 --- /dev/null +++ b/comparison/Python/msort-800000.py @@ -0,0 +1,56 @@ +# Python program for implementation of MergeSort +# https://www.geeksforgeeks.org/merge-sort/ +import random + +def mergeSort(arr): + if len(arr) > 1: + + # Finding the mid of the array + mid = len(arr)//2 + + # Dividing the array elements + L = arr[:mid] + + # into 2 halves + R = arr[mid:] + + # Sorting the first half + mergeSort(L) + + # Sorting the second half + mergeSort(R) + + i = j = k = 0 + + # Copy data to temp arrays L[] and R[] + while i < len(L) and j < len(R): + if L[i] < R[j]: + arr[k] = L[i] + i += 1 + else: + arr[k] = R[j] + j += 1 + k += 1 + + # Checking if any element was left + while i < len(L): + arr[k] = L[i] + i += 1 + k += 1 + + while j < len(R): + arr[k] = R[j] + j += 1 + k += 1 + + +def mkRandList ( n ): + a=[] + for i in range(1,n+1): + a.insert(0, random.randint(0,10000)) + return a + +a = mkRandList(800000) +#a = mkRandList(8) +mergeSort(a) +#print(a) diff --git a/comparison/Python/qsort-800000.py b/comparison/Python/qsort-800000.py new file mode 100644 index 0000000..9a75977 --- /dev/null +++ b/comparison/Python/qsort-800000.py @@ -0,0 +1,34 @@ +# Quicksort +import random + +# https://stackoverflow.com/questions/23767787/python-implementation-of-quicksort-fails-to-sort-the-entire-array +def quickSort(arr): + less = [] + pivotList = [] + more = [] + if len(arr) <= 1: + return arr + else: + pivot = arr[0] + for i in arr: + if i < pivot: + less.append(i) + elif i > pivot: + more.append(i) + else: + pivotList.append(i) + + less = quickSort(less) + more = quickSort(more) + return less + pivotList + more + +def mkRandList ( n ): + a=[] + for i in range(1,n+1): + a.insert(0, random.randint(0,10000)) + return a + +a = mkRandList(800000) +#a = mkRandList(8) +b = quickSort(a) +#print(a) diff --git a/comparison/SML/Makefile b/comparison/SML/Makefile new file mode 100644 index 0000000..8520e9d --- /dev/null +++ b/comparison/SML/Makefile @@ -0,0 +1,15 @@ +COMMAND = sml +TARGET = ack3-11.sml fib38.sml bsort-40000.sml isort-40000.sml qsort-800000.sml msort-800000.sml +BENCHMARK = bash ../tools/sptest_scripts.sh +LOG = log.txt + + +.PHONY: all + +all: $(LOG) + +$(LOG): $(TARGET) + $(BENCHMARK) $(COMMAND) '$(TARGET)' + +clean: + rm -f $(LOG) *~ diff --git a/comparison/SML/ack3-11.sml b/comparison/SML/ack3-11.sml new file mode 100644 index 0000000..f1ea7f2 --- /dev/null +++ b/comparison/SML/ack3-11.sml @@ -0,0 +1,6 @@ +fun ack 0 n = n+1 +| ack m 0 = ack (m-1) 1 +| ack m n = ack (m-1) (ack m (n-1)); + + +ack 3 11; diff --git a/comparison/SML/bsort-40000.sml b/comparison/SML/bsort-40000.sml new file mode 100644 index 0000000..48c3eb4 --- /dev/null +++ b/comparison/SML/bsort-40000.sml @@ -0,0 +1,30 @@ +(* +https://gist.github.com/masquerade0324/68f4f3fd3ab897fbe1ad +*) + +fun bs xs 0 = xs + | bs (x::y::ys) n = + if (x<y) then x::(bs (y::ys) (n-1)) + else y::(bs (x::ys) (n-1)) + | bs xs _ = xs; + +fun go xs 0 = xs + | go xs n = go (bs xs n) (n-1); + + +fun bsort xs = go xs (length(xs) - 1); + + +(* mkRandList *) +local + val nextInt = Random.randRange(1,10000); + val r = Random.rand(1,1); +in + fun mkRandList 0 = [] + | mkRandList n = (nextInt r)::(mkRandList (n-1)) +end; + + +bsort (mkRandList 40000); + + diff --git a/comparison/SML/fib38.sml b/comparison/SML/fib38.sml new file mode 100644 index 0000000..c49ed14 --- /dev/null +++ b/comparison/SML/fib38.sml @@ -0,0 +1,9 @@ +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/SML/isort-40000.sml b/comparison/SML/isort-40000.sml new file mode 100644 index 0000000..973d6e6 --- /dev/null +++ b/comparison/SML/isort-40000.sml @@ -0,0 +1,21 @@ +(* http://www.codecodex.com/wiki/Insertion_sort#Standard_ML *) +fun insertsort [] = [] + | insertsort (x::xs) = + let fun insert (x, []) = [x] + | insert (x, y::ys) = + if x<=y then x::y::ys + else y::insert(x, ys) + in insert(x, insertsort xs) + end; + + +(* mkRandList *) +local + val nextInt = Random.randRange(1,10000); + val r = Random.rand(1,1); +in + fun mkRandList 0 = [] + | mkRandList n = (nextInt r)::(mkRandList (n-1)) +end; + +insertsort (mkRandList 40000);
\ No newline at end of file diff --git a/comparison/SML/msort-800000.sml b/comparison/SML/msort-800000.sml new file mode 100644 index 0000000..2a33649 --- /dev/null +++ b/comparison/SML/msort-800000.sml @@ -0,0 +1,45 @@ +(* https://stackoverflow.com/questions/25839939/understanding-merge-sort-in-ml *) + +fun split [] = ([], []) + | split [a] = ([a], []) + | split (a::b::cs) = + let + val (m,n) = split cs + in + (a::m, b::n) + end; + +fun merge ([], ys) = ys + | merge (xs, []) = xs + | merge (x::xs, y::ys) = + if x<y then + x::merge(xs, y::ys) + else + y::merge(x::xs, ys); + +fun msort [] = [] + | msort [a] = [a] + | msort [a,b] = if a<b then [a,b] else [b,a] + | msort xs = + let + val (m,n) = split xs; + in + merge (msort m, msort n) + end; + + + + +(* mkRandList *) +local + val nextInt = Random.randRange(1,10000); + val r = Random.rand(1,1); +in + fun mkRandList 0 = [] + | mkRandList n = (nextInt r)::(mkRandList (n-1)) +end; + + +msort (mkRandList 800000); + + diff --git a/comparison/SML/qsort-800000.sml b/comparison/SML/qsort-800000.sml new file mode 100644 index 0000000..e27af56 --- /dev/null +++ b/comparison/SML/qsort-800000.sml @@ -0,0 +1,27 @@ +fun quicksort nil = nil + | quicksort (pivot :: rest) = + let + fun split(nil) = (nil,nil) + | split(x :: xs) = + let + val (below, above) = split(xs) + in + if x < pivot then (x :: below, above) + else (below, x :: above) + end; + val (below, above) = split(rest) + in + quicksort below @ [pivot] @ quicksort above + end; + + +(* mkRandList *) +local + val nextInt = Random.randRange(1,10000); + val r = Random.rand(1,1); +in + fun mkRandList 0 = [] + | mkRandList n = (nextInt r)::(mkRandList (n-1)) +end; + +quicksort (mkRandList 800000); diff --git a/comparison/all.sh b/comparison/all.sh new file mode 100644 index 0000000..424e50f --- /dev/null +++ b/comparison/all.sh @@ -0,0 +1,8 @@ +#!/usr/bin/bash +dir=(Haskell SML Python Inpla) +for d in ${dir[@]} ; do +# echo "cd $d; make clean; make; cd .."; +# cd $d; make clean; make; cd .. + echo "cd $d; make; cd .."; + cd $d; make; cd .. +done diff --git a/comparison/all.sh~ b/comparison/all.sh~ new file mode 100644 index 0000000..126d37f --- /dev/null +++ b/comparison/all.sh~ @@ -0,0 +1,6 @@ +#!/usr/bin/bash +dir=(Haskell SML Python) +for d in ${dir[@]} ; do + echo "cd $d; make clean; make; cd .."; + cd $d; make clean; make; cd .. +done diff --git a/comparison/tools/sptest_commands.sh b/comparison/tools/sptest_commands.sh new file mode 100644 index 0000000..d608d2c --- /dev/null +++ b/comparison/tools/sptest_commands.sh @@ -0,0 +1,64 @@ +#!/bin/bash +# $1: name list of commands +# ex: ./sptest_commands.sh fib38 bsort-40000 + + +# repeat and waittimes +ct=10 +waittime=5 + +# etc +log="log.txt" + +# ------------------------------------------------- +function mywait() { + for w in $(seq 1 $waittime) ; do + echo -n "." + sleep 1 + done +} + +function gettime() { + rm -f _result.txt + for i in $(seq 1 $ct) ; do + (time $1) 2>> _result.txt + echo + echo "Done ($i/$ct)" + if [[ $(($i + 1)) -le $ct ]]; then + mywait + echo 'Restart' + fi + done + echo + echo + logtime=`grep real _result.txt | cut -f2 | sed -e "s/\(.*\)m\(.*\)s/\1 \2/" | awk -F' ' '{print ($1*60+$2)}'` + echo "execution time" + echo "$logtime" + + # for return value + res=`echo $logtime | awk 'BEGIN{r=0;c=0} {r=r+$0;c++} END{print r/c}'` + rm -f _result.txt +} + + +rm -f $log + +echo '' >> $log +echo "$ct" | awk -F',' '{printf("%-27s| avg. time over %d runs (sec)\n","",$1)}' >> $log +echo '===========================+=============================' >> $log + +for f in "$@"; do + command="$f" + echo -n "Cooling CPU in $waittime sec" + mywait + echo ' The experiment starts.' + echo "------------------" + echo "$f" + gettime "./$f" + echo "Average ${res}sec" + echo "$command,$res" | awk -F',' '{printf("%-27s| %.2f\n",$1,$2)}' >> $log +done + +echo +cat $log + diff --git a/comparison/tools/sptest_commands.sh~ b/comparison/tools/sptest_commands.sh~ new file mode 100644 index 0000000..0451c4e --- /dev/null +++ b/comparison/tools/sptest_commands.sh~ @@ -0,0 +1,58 @@ +#!/bin/bash +# $1: name list of commands +# ex: ./sptest_commands.sh fib38 bsort-40000 + + +# repeat and waittimes +ct=10 +waittime=5 + +# etc +log="log.txt" + +# ------------------------------------------------- +function gettime() { + rm -f _result.txt + for ((i=1; i<ct; i++)) + do + (time $1) 2>> _result.txt + echo + echo "Done ($i/$ct)" + sleep $waittime + echo 'Restart' + done + (time $1) 2>> _result.txt + echo + echo + logtime=`grep real _result.txt | awk -F'\t' '{print $2}' | cut -c3- | awk -F's' '{print $1}'` + echo "Execution times" + echo "$logtime" + res=`echo "$logtime\n" | awk 'BEGIN{r=0;c=0} {r=r+$0;c++} END{print r/c}'` + + # for return value + res=`grep real _result.txt | awk -F'\t' '{print $2}' | cut -c3- | awk -F's' '{print $1}' | awk 'BEGIN{r=0;c=0} {r=r+$0;c++} END{print r/c}'` + rm -f _result.txt +} + + +rm -f $log + +echo '' >> $log +echo "$ct" | awk -F',' '{printf("%-27s| avg. time over %d runs (sec)\n","",$1)}' >> $log +echo '===========================+=============================' >> $log + +for f in "$@"; do + command="$f" + echo 'Now just cooling CPU...' + sleep $waittime + echo 'The experimental was started.' + echo "------------------" + echo "$f" + gettime "./$f" + echo "Average ${res}sec" + echo "$command,$res" | awk -F',' '{printf("%-27s| %.2f\n",$1,$2)}' >> $log +done + +echo +cat $log + diff --git a/comparison/tools/sptest_scripts.sh b/comparison/tools/sptest_scripts.sh new file mode 100644 index 0000000..7d384fe --- /dev/null +++ b/comparison/tools/sptest_scripts.sh @@ -0,0 +1,69 @@ +#!/bin/bash +# $1: executable file name +# $2: name list of script files +# ex: ./sptest_scripts.sh sml 'fib5.sml fib10.sml' +# Then, 'sml < fib5.sml' and "sml < fib10.sml' are executed. + + +# repeat and waittimes +ct=10 +waittime=5 + +# etc +comf=$1 +log="log.txt" +sfs=($2); # sourcefiles + +# ------------------------------------------------- +function mywait() { + for w in $(seq 1 $waittime) ; do + echo -n "." + sleep 1 + done +} + +function gettime() { + rm -f _result.txt + for i in $(seq 1 $ct) ; do + (time $1 < $2) 2>> _result.txt + echo + echo -n "Done ($i/$ct)" + if [[ $(($i + 1)) -le $ct ]]; then + mywait + echo 'Restart' + fi + done + echo + echo + logtime=`grep real _result.txt | cut -f2 | sed -e "s/\(.*\)m\(.*\)s/\1 \2/" | awk -F' ' '{print ($1*60+$2)}'` + echo "execution time" + echo "$logtime" + + # for return value + res=`echo $logtime | awk 'BEGIN{r=0;c=0} {r=r+$0;c++} END{print r/c}'` + rm -f _result.txt +} + + +rm -f $log + +echo '' >> $log +echo "$ct" | awk -F',' '{printf("%-27s| avg. time over %d runs (sec)\n","",$1)}' >> $log +echo '===========================+=============================' >> $log + +for f in "${sfs[@]}" +do + command="$f" + echo -n "Cooling CPU in $waittime sec" + mywait + echo ' The experiment starts.' + echo "------------------" + echo "$comf < $f" + gettime "$comf" "$f" + echo "Average ${res}sec" + echo + echo "$command,$res" | awk -F',' '{printf("%-27s| %.2f\n",$1,$2)}' >> $log +done +echo +cat $log + diff --git a/comparison/tools/sptest_scripts.sh~ b/comparison/tools/sptest_scripts.sh~ new file mode 100644 index 0000000..2d543d0 --- /dev/null +++ b/comparison/tools/sptest_scripts.sh~ @@ -0,0 +1,63 @@ +#!/bin/bash +# $1: executable file name +# $2: name list of script files +# ex: ./sptest_scripts.sh sml 'fib5.sml fib10.sml' +# Then, 'sml < fib5.sml' and "sml < fib10.sml' are executed. + + +# repeat and waittimes +#ct=10 +ct=1 +waittime=5 + +# etc +comf=$1 +log="log.txt" +sfs=($2); # sourcefiles + +# ------------------------------------------------- +function gettime() { + rm -f _result.txt + for ((i=1; i<ct; i++)) + do + (time $1 < $2) 2>> _result.txt + echo + echo "Done ($i/$ct)" + sleep $waittime + echo 'Restart' + done + (time $1 < $2) 2>> _result.txt + echo + echo + logtime=`grep real _result.txt | cut -f2 | sed -e "s/\(.*\)m\(.*\)s/\1 \2/" | awk -F' ' '{print ($1*60+$2)}'` + echo "Execution times" + echo "$logtime" + res=`echo "$logtime\n" | awk 'BEGIN{r=0;c=0} {r=r+$0;c++} END{print r/c}'` + + # for return value + res=`grep real _result.txt | awk -F'\t' '{print $2}' | cut -c3- | awk -F's' '{print $1}' | awk 'BEGIN{r=0;c=0} {r=r+$0;c++} END{print r/c}'` + rm -f _result.txt +} + + +rm -f $log + +echo '' >> $log +echo "$ct" | awk -F',' '{printf("%-27s| avg. time over %d runs (sec)\n","",$1)}' >> $log +echo '===========================+=============================' >> $log + +for f in "${sfs[@]}" +do + command="$f" + echo 'Now just cooling CPU...' + sleep $waittime + echo 'The experimental was started.' + echo "------------------" + echo "$comf < $f" + gettime "$comf" "$f" + echo "Average ${res}sec" + echo "$command,$res" | awk -F',' '{printf("%-27s| %.2f\n",$1,$2)}' >> $log +done +echo +cat $log + |
