我想在一些不同的条件下在linux shell中执行一些东西,并且能够输出每次执行的执行时间。

我知道我可以写一个perl或python脚本,可以做到这一点,但有一种方法我可以在shell中做到这一点吗?(恰好是bash)


当前回答

如果您只需要精确到秒,您可以使用内置的$SECONDS变量,该变量计算shell已经运行的秒数。

while true; do
    start=$SECONDS
    some_long_running_command
    duration=$(( SECONDS - start ))
    echo "This run took $duration seconds"
    if some_condition; then break; fi
done

其他回答

#!/bin/bash
START=$(date +%s)
# do something
# start your script work here
ls -R /etc > /tmp/x
rm -f /tmp/x
# your logic ends here
END=$(date +%s)
DIFF=$(( $END - $START ))
echo "It took $DIFF seconds"

方法是

$ > g++ -lpthread perform.c -o per
$ > time ./per

输出为>>

real    0m0.014s
user    0m0.010s
sys     0m0.002s

perf stat Linux命令行实用程序

这个工具对于获取时间来说是多余的。但是它可以为您做更多的事情,帮助您分析和修复速度慢,这是值得了解的。Ubuntu 22.04安装:

sudo apt install linux-tools-common linux-tools-generic
echo -1 | sudo tee /proc/sys/kernel/perf_event_paranoid

用法:

perf stat <mycmd>

使用stress-ng运行示例:

perf stat stress-ng --cpu 1 --cpu-method matrixprod -t 5

样例输出:

 Performance counter stats for 'stress-ng --cpu 1 --cpu-method matrixprod -t 5':

          5,005.46 msec task-clock                #    0.999 CPUs utilized          
                88      context-switches          #   17.581 /sec                   
                 1      cpu-migrations            #    0.200 /sec                   
             1,188      page-faults               #  237.341 /sec                   
    18,847,667,167      cycles                    #    3.765 GHz                    
    26,544,261,897      instructions              #    1.41  insn per cycle         
     3,239,655,001      branches                  #  647.225 M/sec                  
        25,393,369      branch-misses             #    0.78% of all branches        

       5.012218939 seconds time elapsed

       4.998051000 seconds user
       0.009122000 seconds sys

perf还可以做一些更高级的事情,例如,在这里我将展示如何使用它来分析代码:我如何分析运行在Linux上的c++代码?

你可以使用time和subshell ():

time (
  for (( i=1; i<10000; i++ )); do
    echo 1 >/dev/null
  done
)

或者在同一个shell{}中:

time {
  for (( i=1; i<10000; i++ )); do
    echo 1 >/dev/null
  done
}

如果您只需要精确到秒,您可以使用内置的$SECONDS变量,该变量计算shell已经运行的秒数。

while true; do
    start=$SECONDS
    some_long_running_command
    duration=$(( SECONDS - start ))
    echo "This run took $duration seconds"
    if some_condition; then break; fi
done