FILE=word_list
#DICT=linux.words.all4
DICT=/usr/share/dict/linux.words

MODE=cat
#MODE=tail	#smaller sample size

SLEEP_TIME=2

clear
echo "Comparing runtime of serial and 3 parallel processing methods...parallel, xargs and a for-loop"
echo

echo "Run mode: ${MODE}"
echo

echo "Kernel"
uname -r
echo

echo "Machine profile"
sudo dmidecode | egrep -i 'product|cpu|core|thread'
echo;echo

# compare the serially, parallelly and for loop below
#

#
if [ $MODE == 'tail' ]; then
 echo serially...;time for i in `tail -1000 ${FILE}`; do grep -Fw $i ${DICT}>/dev/null; done  # serially 

 sleep ${SLEEP_TIME};echo;echo parallel...;time tail -1000 ${FILE} | parallel grep -Fw {} ${DICT}>/dev/null

 sleep ${SLEEP_TIME};echo;echo xargs...;time tail -1000 ${FILE} | xargs -P0 -I % sh -c "grep -Fw % ${DICT}">/dev/null

 sleep ${SLEEP_TIME};echo;echo for-loop...;rm -f para.sh; for i in `tail -1000 ${FILE}`; do echo "grep -Fw $i ${DICT}>/dev/null&">>para.sh; done; echo wait>>para.sh; time sh para.sh   # parallelly 
fi


#
if [ $MODE == 'cat' ]; then
 echo "serially... in this mode processing takes too long...skipping"
 #clear;echo serially...;time for i in `cat ${FILE}`; do grep -Fw $i ${DICT}>/dev/null; done  # serially, too sloooow.

 sleep ${SLEEP_TIME};echo;echo parallel...;time cat ${FILE} | parallel grep -Fw {} ${DICT}>/dev/null

 sleep ${SLEEP_TIME};echo;echo xargs...;time cat ${FILE} | xargs -P0 -I % sh -c "grep -Fw % ${DICT}">/dev/null

 sleep ${SLEEP_TIME};echo;echo for-loop...;rm -f para.sh; for i in `cat ${FILE}`; do echo "grep -Fw $i ${DICT}>/dev/null&">>para.sh; done; echo wait>>para.sh; time sh para.sh   # parallelly 
fi

