显示编译代码时长的demo

来源:互联网 发布:淘宝怎么认证代购 编辑:程序博客网 时间:2024/04/27 22:03

1.创建time.sh

 #!/bin/bash    
 start_time=$(date +"%s")
 $(echo command make) "$@" #注意:$@表示目标,就是编译要生成的可执行文件或image.

                           #这里调用make命令,执行./time.sh ramdisk,相当于执行:make ramdisk命令。也可以添加其他的命令:ls,cat...
 ret=$?
 end_time=$(date +"%s")
 tdiff=$(($end_time-$start_time))
 hours=$(($tdiff / 3600 ))
 mins=$((($tdiff % 3600) / 60))
 secs=$(($tdiff % 60))
echo
 if [ $ret -eq 0 ] ; then
     echo -n -e "#### make completed successfully "
 else
     echo -n -e "#### make failed to build some targets "
 fi
 
 if [ $hours -gt 0 ] ; then
     printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
 elif [ $mins -gt 0 ] ; then
     printf "(%02g:%02g (mm:ss))" $mins $secs
     #elif [ $secs -gt 0 ] ; then
 else
     printf "(%s seconds)" $secs
 fi
 echo -e " ####"
 echo


3.执行命令

   # ./time.sh ramdisk

  即显示编译时长.


0 0