Skip to content

Instantly share code, notes, and snippets.

@catichenor
Last active February 25, 2016 00:34
Show Gist options
  • Save catichenor/d1a14bfbbac30c0de309 to your computer and use it in GitHub Desktop.
Save catichenor/d1a14bfbbac30c0de309 to your computer and use it in GitHub Desktop.
How to average the performance results of several runs on a particular command in Bash
#!/bin/bash
numberOfRuns="10"
i="0"
while [ $i -lt ${numberOfRuns} ]
do
results[${i}]=`./performanceTest` # Replace ./performanceTest with the command line of the test you want to run.
# ^ This assumes that the result of the test will be a number (integer or decimal).
echo "Result this run: ${results[${i}]}"
i=$[$i+1]
done
resultTotal="0"
for thisResult in "${results[@]}"
do
resultTotal=`echo "${resultTotal}+${thisResult}" | bc`
done
average=`echo "scale=4;${resultTotal}/${numberOfRuns}" | bc` # "scale" controls the decimal places used for the division calculation.
echo "The average is: ${average}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment