前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >linux程序运行耗时shell脚本running_time.sh

linux程序运行耗时shell脚本running_time.sh

作者头像
Michael阿明
发布2021-02-20 10:22:04
发布2021-02-20 10:22:04
2.7K00
代码可运行
举报
运行总次数:0
代码可运行

对一个程序进行多次运行,求其平均运行时间

代码语言:javascript
代码运行次数:0
复制
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 )).$(( (10#$end_nanos - 10#$start_nanos)/10**6 ))
    avgtime=`echo "sclae=4; $time/$n" | bc -l`
    echo $avgtime
}

starttime=`date +'%s.%N'`

i=0
n=5
while [ $i -lt 5 ] #循环,把两个5改成想要运行的次数
do
    ./a.out big quicksort1_opti2  #需要运行的程序
    wait
    let i=i+1
done
wait # 等待执行完成 即可
endtime=`date +'%s.%N'`

echo "程序平均运行时间: "  
timediff $starttime $endtime #比较开始和结束时间的差
echo " s"
exit 0

./running_time.sh >> result.txt

可以使结果写入文本中

nohup ./running_time.sh >> result.txt &

转入后台运行,终端可以继续干别的

jobs 命令查看后台程序

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/08/19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档