对一个程序进行多次运行,求其平均运行时间
function timediff() {# time format:date +"%s.%N", such as 1502758855.907197692start_time=$1end_time=$2start_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 decimalif [ "$end_nanos" -lt "$start_nanos" ];thenend_s=$(( 10#$end_s - 1 ))end_nanos=$(( 10#$end_nanos + 10**9 ))fi# get timedifftime=$(( 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=0n=5while [ $i -lt 5 ] #循环,把两个5改成想要运行的次数do./a.out big quicksort1_opti2 #需要运行的程序waitlet i=i+1donewait # 等待执行完成 即可endtime=`date +'%s.%N'`echo "程序平均运行时间: " timediff $starttime $endtime #比较开始和结束时间的差echo " s"exit 0
./running_time.sh >> result.txt
可以使结果写入文本中
nohup ./running_time.sh >> result.txt &
转入后台运行,终端可以继续干别的
jobs
命令查看后台程序
nohup python -u TargetRecongnizer-pt.py > out 2>&1 &
-u
不缓存,实时输出到 out 文件>>
追加,>
覆盖
如果觉得《linux程序运行耗时shell脚本running_time.sh》对你有帮助,请点赞、收藏,并留下你的观点哦!