Skip to content

Instantly share code, notes, and snippets.

@ditatompel
Created August 16, 2022 01:56
Show Gist options
  • Save ditatompel/8a9714195ba81066a7fbf44f6968b103 to your computer and use it in GitHub Desktop.
Save ditatompel/8a9714195ba81066a7fbf44f6968b103 to your computer and use it in GitHub Desktop.
Simple bash script to check execution time of given binary from CS50 Harvard University Lab #3
#!/bin/bash
# Simple bash script to check execution time of given
# binary from CS50 Harvard University Lab #3
#
# Make sure you download correct .zip file, extract and
# place this script to ./sort directory.
# Please see https://cs50.harvard.edu/x/2022/labs/3/ for
# more details.
#
# NOTE:
# You need Bash version 4.2 or above to run this script
# because lastpipe introduced since Bash 4.2
shopt -s lastpipe
binfile=(
"./sort1"
"./sort2"
"./sort3"
)
for bin in "${binfile[@]}"
do
echo "$bin"
totalruntime=0;
sortedtime=0;
reversedtime=0;
randomtime=0;
find ./ -type f -name "*000.txt" -print0 | while read -d $'\0' file
do
start=`date +%s.%N`
$bin $file > /dev/null 2>&1
end=`date +%s.%N`
runtime=$(echo "$end - $start" | bc -l)
if [[ $file =~ .*sorted.* ]]; then
sortedtime=$(echo "$sortedtime + $runtime" | bc -l)
fi
if [[ $file =~ .*reversed.* ]]; then
reversedtime=$(echo "$reversedtime + $runtime" | bc -l)
fi
if [[ $file =~ .*random.* ]]; then
randomtime=$(echo "$randomtime + $runtime" | bc -l)
fi
totalruntime=$(echo "$totalruntime + $runtime" | bc -l)
echo "$runtime sec exec time for $file"
done
echo "---------------------------";
echo "Sorted time: : $sortedtime";
echo "Reversed time : $reversedtime";
echo "Random time: : $randomtime";
echo "Total runtime : $totalruntime";
echo "---------------------------";
echo "===========================";
done
# vim: number tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment