Shell program timing

Shell program timing

Sometimes we write a pipeline and we want to know how long it will run. This needs to implement the timing function, which can be implemented with date or time.

1.date

First use man date to view the following instructions:

image.png

It is easier to understand with a few examples:

date "+DATE: %Y-%m-%d%nTIME: %H:%M:%S"

Output: DATE: 2020-01-13 TIME: 10:48:28 (current time)

date -v1m

Output: Monday, January 13, 2020 10:50:20 CST

 date "+%m%d%H%M%S"

Output: 0113105443 that is 10:54:43 on January 13th.

For example, the following script timing:

start=`date +%s` # %s can calculate the number of seconds since 1970
sleep 20 
end=`date +%s`

time=`echo $start $end | awk'{print $2-$1}'`
echo $time

Can output 20.

2. Time time can calculate the running time of a certain program (real), user mode cpu time (user), and system mode cpu time (sys). For example, test the time of the main function sleep 20 above:

time sleep 20

Output: real 0m20.007s user 0m0.002s sys 0m0.003s

If you test the script, the format is time sh xxx.sh.

Welcome to pay attention to the official account: daily life letter programming

Reference: https://cloud.tencent.com/developer/article/1607864 Shell program timing-Cloud + Community-Tencent Cloud