Skip to content

Latest commit

 

History

History
54 lines (39 loc) · 1.22 KB

File metadata and controls

54 lines (39 loc) · 1.22 KB

README

Demonstrate how to time operations in the shell to help with optimisation.

Reasons

Always a good idea to understand about the performance of scripts. Understanding if it meets expectations.

Example usage can be found here

TODO:

Use EPOCHREALTIME

Simple example of measuring sleep 10 command

# microsecond 1 millionth of second
start=${EPOCHREALTIME}
sleep 10
end=${EPOCHREALTIME}
runtime=$(bc <<< "(${end} - ${start})")
echo "${runtime} seconds"

Use time

Simple example of measuring sleep 10 command

time sleep 10     

Time it over iterations

# add a custom function to time 
function under_test() {
    echo "Parameters $*"
    local sleeptime=1
    if [[ -n $1 ]]; then
        sleeptime=$1
    fi
    echo "sleep $sleeptime"
    sleep $sleeptime
}

# pass function to test, number of iterations and parameters to pass into the function (sleeptime of 2 in this case)
. ./time-it.sh under_test 10 2