Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:51:52

0001 #!/bin/bash
0002 
0003 # Functions to compute average and standard deviation from a list of integers
0004 
0005 # returns the average of a list of integers
0006 avg() {
0007     local RESULT="$1"
0008     local SUM=0
0009     local COUNT=0
0010     for PARAM in $2;
0011     do
0012       set -- $PARAM
0013       SUM=$(($SUM+$1))
0014       COUNT=$(($COUNT+1))
0015     done
0016     
0017     if [ $COUNT == 0 ]
0018         then
0019         echo "Error:  avg():  empty list"
0020         return -1
0021     fi
0022     
0023     eval "$RESULT=\"$(echo "scale=4; $SUM/$COUNT" | bc)\""
0024     
0025     
0026     return 0
0027 }
0028 # returns the standard deviation of a list of integers
0029 sdv() {
0030     local RESULT="$1"
0031     local AVG=0
0032     avg AVG "$2"
0033     local DIFF=0
0034     local SUMDIFF2=0
0035     local COUNT=0
0036     for PARAM in $2;
0037     do
0038       set -- $PARAM
0039       DIFF=$(echo "scale=4; $1-$AVG" | bc)
0040       SUMDIFF2=$(echo "scale=4; $SUMDIFF2 + $DIFF ^2" | bc)
0041       COUNT=$(($COUNT+1))
0042     done
0043 
0044     if [ $COUNT == 0 ]
0045         then
0046         echo "Error:  sdv():  empty list"
0047         return -1
0048     fi
0049 
0050     eval "$RESULT=\"$(echo "scale=4; sqrt($SUMDIFF2/$COUNT)" | bc)\""
0051     return 0
0052 }
0053 # returns both the average and the standard deviation
0054 stats() {
0055     local AVGRESULT="$1"
0056     local SDVRESULT="$2"
0057     local DATALIST="$3"
0058     local AVG=0
0059     avg AVG "$DATALIST"
0060 
0061     local DIFF=0
0062     local SUMDIFF2=0
0063     local COUNT=0
0064     for PARAM in $DATALIST;
0065     do
0066       set -- $PARAM
0067       DIFF=$(echo "scale=4; $1-$AVG" | bc)
0068       SUMDIFF2=$(echo "scale=4; $SUMDIFF2 + $DIFF ^2" | bc)
0069       COUNT=$(($COUNT+1))
0070     done
0071 
0072     if [ $COUNT == 0 ]
0073         then
0074         echo "Error:  stats():  empty list"
0075         return -1
0076     fi
0077 
0078     eval "$AVGRESULT=\"$AVG\""
0079     eval "$SDVRESULT=\"$(echo "scale=4; sqrt($SUMDIFF2/$COUNT)" | bc)\""
0080     return 0
0081 }
0082 
0083 runx() {
0084     local COMMAND=$1
0085     local LIMIT=$2
0086     local COUNT=0
0087     local T1=0
0088     local T2=0
0089     local DIFFTIME=0
0090     local LIST=""
0091 
0092     echo "[---TIMING LOG---] Running \"$COMMAND\" $LIMIT times"
0093     
0094     while [ "$COUNT" -lt "$LIMIT" ]
0095     do
0096       T1=`date +%s`
0097       $COMMAND
0098       T2=`date +%s`
0099       DIFFTIME=$(($T2-$T1))
0100       echo "[---TIMING LOG---] Trial $COUNT:  $DIFFTIME seconds"
0101       LIST="$DIFFTIME $LIST"
0102       COUNT=$(($COUNT+1))
0103       sleep 10
0104     done
0105 
0106     local AVGTIME=0
0107     local SDVTIME=0
0108     stats AVGTIME SDVTIME "$LIST"
0109     echo "[---TIMING LOG---] Execution time:  $AVGTIME +- $SDVTIME seconds"
0110     
0111     return 0
0112 }