计算shell脚本的执行时间

一. 秒级别

#!/bin/bash

start_time=`date --date='0 days ago' "+%Y-%m-%d %H:%M:%S"`
 
#this is your shell script 
sleep 1
 
##############
finish_time=`date --date='0 days ago' "+%Y-%m-%d %H:%M:%S"`
 
duration=$(($(($(date +%s -d "$finish_time")-$(date +%s -d "$start_time")))))
 
echo "this shell script execution duration: $duration"

执行如下命令:

chmod +x time_s.sh
./time_s.sh

结果:

二.毫秒级别

#!/bin/bash

function timediff() {
# time format:date +"%s.%N", such as 1502758855.907197692
    start_time=$1
    end_time=$2

    start_s=${start_time%.*}
    start_nanos=${start_time#*.}
    end_s=${end_time%.*}
    end_nanos=${end_time#*.}

    # end_nanos > start_nanos? 
    # Another way, the time part may start with 0, which means
    # it will be regarded as oct format, use "10#" to ensure
    # calculateing with decimal

    if [ "$end_nanos" -lt "$start_nanos" ];then
        end_s=$(( 10#$end_s - 1 ))
        end_nanos=$(( 10#$end_nanos + 10**9 ))
    fi

# get timediff
    time=$(( 10#$end_s - 10#$start_s )).`printf "%03dn" $(( (10#$end_nanos - 10#$start_nanos)/10**6 ))`
    echo $time
}

start=$(date +"%s.%N")

# Now exec some command
sleep 1.2563

end=$(date +"%s.%N")
# here give the values

#start=1502758855.907197692

#end=1502758865.066894173


timediff $start $end

执行如下命令:

chmod +x time_s.sh
./time_ms.sh

结果:

 

参考:

https://blog.csdn.net/u010339879/article/details/75949076

https://www.lagou.com/lgeduarticle/2266.html

https://blog.csdn.net/weixin_34050427/article/details/85934782

计算shell脚本执行时间,可以使用date命令或者time工具。使用date命令,可以在脚本的开始和结束位置分别获取当前时间,并计算时间差,从而得到执行时间。例如: ``` #!/bin/bash startTime=$(date "+%Y%m%d-%H:%M:%S") startTime_s=$(date "+%s") # 在这里写下你的脚本代码 endTime=$(date "+%Y%m%d-%H:%M:%S") endTime_s=$(date "+%s") sumTime=$((endTime_s - startTime_s)) echo "$startTime ---> $endTime" "Total: $sumTime seconds" ``` 另外,你还可以使用time工具来测量脚本执行时间执行`time sh your_script.sh`命令会返回三个时间数据:real、user和sys。real表示命令的总耗时,包括用户模式和内核模式下的CPU耗时、I/O等待等;user表示命令在用户模式下的CPU耗时,不包括I/O等待;sys表示命令在内核中的CPU耗时,不包括I/O等待和时间片切换耗时。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [shell脚本限制命令执行时间,设置超时时间](https://blog.csdn.net/qq_36470898/article/details/118309502)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [【ShellShell时间相关|统计Shell脚本执行时间|sleep](https://blog.csdn.net/bandaoyu/article/details/115525067)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值