Bash 在unix脚本中执行命令并计算计时

Bash 在unix脚本中执行命令并计算计时,bash,unix,Bash,Unix,我对bash脚本非常陌生。如何执行下面的命令以捕获执行该命令的时间 我在数组变量中捕获了我的交易。然后,我在交易中循环执行TradeService远程调用。我想捕捉这次通话的时间,并将其汇总到所有交易中 执行此调用并在脚本中捕获时间的最佳方法是什么 如何在脚本中管理时间,以小时/分/秒为单位显示时间的后期聚合 #/bin/bash DEALS=“1739719、1714630、1733697、1723940、1666257、1665239” 如果s=',read-ra array您可以在操作前后

我对bash脚本非常陌生。如何执行下面的命令以捕获执行该命令的时间

我在数组变量中捕获了我的交易。然后,我在交易中循环执行TradeService远程调用。我想捕捉这次通话的时间,并将其汇总到所有交易中

  • 执行此调用并在脚本中捕获时间的最佳方法是什么
  • 如何在脚本中管理时间,以小时/分/秒为单位显示时间的后期聚合
  • #/bin/bash
    DEALS=“1739719、1714630、1733697、1723940、1666257、1665239”
    
    如果s=',read-ra array您可以在操作前后使用
    date+%s
    查找以秒为单位的时间差。例如:

    array=( 5 10 15 20 25 )
    for val in "${array[@]}"
    do
     starttime=$(date +%s)
     sleep "$val" # sleeping for '$val' seconds in each iteration
     #you should replace the above with the actual process.
     endtime=$(date +%s)
     echo "Time taken for the process is $((endtime-starttime))s"
    done
    
    这将给

    Time taken for the process is 5s
    Time taken for the process is 10s
    Time taken for the process is 15s
    Time taken for the process is 20s
    Time taken for the process is 25s
    
    然而,我应该承认,它可以提供高达秒的精度

    +%s
    格式字符串需要特别提及。
    日期
    手册页显示:

    %自1970-01-01 00:00:00 UTC以来的秒数

    本质上,我们只是计算过去相对于参考点经过的时间(以秒为单位),然后计算结束时间和开始时间之间的差异。一旦你有了以秒为单位的时间,使用定义明确的数学将小时和分钟分开

    正如@ChalresDuffy在评论中指出的,您可以使用内部变量来计算时差。如果需要微秒的粒度,也可以使用。

    原始答案

    #!/bin/bash
    
    DEALS="1739719, 1714630, 1733697, 1723940, 1666257, 1665239"
    
    IFS=', ' read -ra array <<< "$DEALS"
    total=0
    
    for i in "${array[@]}" ; do
        echo "Executing for DEALS $i"
    
        startdeals=$(date +%s)
        java -jar ~/support/lib/polyglot.jar --command=call --endpoint=trades-server:10443 --full_method='TradeService/GetDeals' > /dev/null
        enddeals=$(date +%s)
        let "diff=$enddeals - $startdeals"
        let "total=$total + $diff"
        echo "Runtime for $i : $(date -d@$diff -u +%H:%M:%S)"
    done
    
    echo "Overall execution time: $(date -d@$total -u +%H:%M:%S)"
    

    如果您只需要秒级分辨率,为什么不直接使用
    $seconds
    获取shell开始执行后的秒数?这也比运行
    date
    @CharlesDuffy对性能的影响要小得多。你是对的,根据编辑。我通常建议
    diff=$((enddeals-startdeals))
    而不是非标准化的
    let
    (bash与ksh88向后兼容,ksh88早于1992年的POSIX sh标准)。另请参见shell是bash,因此我们有
    $SECONDS
    ,不对?@CharlesDuffy,这是正确的。而且它也更高效。EPOCHREALTIME可能也很有趣。您之前提出的问题的重复部分探讨了为什么在作业中
    =
    周围不能有空格。用于捕获
    时间的输出y、 另见。
    
    #!/bin/bash
    
    DEALS="1739719, 1714630, 1733697, 1723940, 1666257, 1665239"
    
    IFS=', ' read -ra array <<< "$DEALS"
    total=0
    
    for i in "${array[@]}" ; do
        echo "Executing for DEALS $i"
    
        startdeals=$(date +%s)
        java -jar ~/support/lib/polyglot.jar --command=call --endpoint=trades-server:10443 --full_method='TradeService/GetDeals' > /dev/null
        enddeals=$(date +%s)
        let "diff=$enddeals - $startdeals"
        let "total=$total + $diff"
        echo "Runtime for $i : $(date -d@$diff -u +%H:%M:%S)"
    done
    
    echo "Overall execution time: $(date -d@$total -u +%H:%M:%S)"
    
    #!/bin/bash
    
    DEALS="1739719, 1714630, 1733697, 1723940, 1666257, 1665239"
    
    IFS=', ' read -ra array <<< "$DEALS"
    
    for i in "${array[@]}" ; do
        echo "Executing for DEALS $i"
    
        startdeals="$SECONDS"
        java -jar ~/support/lib/polyglot.jar --command=call --endpoint=trades-server:10443 --full_method='TradeService/GetDeals' > /dev/null
        enddeals="$SECONDS"
        diff=$(( enddeals - startdeals ))
        echo "Runtime for $i : $diff seconds."
    done
    
    echo "Overall execution time: $SECONDS seconds."