如何在 shell 脚本中测量持续时间(以秒为单位)?

发布于 2024-10-19 17:15:16 字数 44 浏览 2 评论 0原文

我想知道 Linux shell 脚本中一个操作需要多长时间。我该怎么做?

I wish to find out how long an operation takes in a Linux shell script. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无语# 2024-10-26 17:15:16

正如其他人所建议的那样,使用 time 命令是一个好主意。

另一种选择是使用神奇的内置变量 $SECONDS,它包含自脚本开始执行以来的秒数。您可以说:

START_TIME=$SECONDS
dosomething
ELAPSED_TIME=$(($SECONDS - $START_TIME))

我认为这是特定于 bash 的,但由于您使用的是 Linux,所以我假设您正在使用 bash。

Using the time command, as others have suggested, is a good idea.

Another option is to use the magic built-in variable $SECONDS, which contains the number of seconds since the script started executing. You can say:

START_TIME=$SECONDS
dosomething
ELAPSED_TIME=$(($SECONDS - $START_TIME))

I think this is bash-specific, but since you're on Linux, I assume you're using bash.

熟人话多 2024-10-26 17:15:16

使用时间命令。 时间 ls /bin

Use the time command. time ls /bin.

月牙弯弯 2024-10-26 17:15:16

尝试以下示例:

START_TIME=$SECONDS
# do something
sleep 65

ELAPSED_TIME=$(($SECONDS - $START_TIME))

echo "$(($ELAPSED_TIME/60)) min $(($ELAPSED_TIME%60)) sec"    
#> 1 min 5 sec

Try following example:

START_TIME=$SECONDS
# do something
sleep 65

ELAPSED_TIME=$(($SECONDS - $START_TIME))

echo "$(($ELAPSED_TIME/60)) min $(($ELAPSED_TIME%60)) sec"    
#> 1 min 5 sec
守不住的情 2024-10-26 17:15:16

许多答案都提到 $SECONDS,但该变量实际上是 比他们意识到的还要好

对该变量赋值会将计数重置为分配的值,并且扩展值变为分配的值加上自分配以来的秒数。

这意味着您可以直接在脚本末尾直接查询该变量来打印经过的时间:

#!/usr/bin/env bash

# Do stuff...

echo "Script finished in $SECONDS seconds."

您还可以为较小的部分计时,如下所示:

#!/usr/bin/env bash

# Do stuff

SECONDS=0

# Do timed stuff...

echo "Timed stuff finished in $SECONDS seconds."

Many of the answers mention $SECONDS, but that variable is actually even better than they realize:

Assignment to this variable resets the count to the value assigned, and the expanded value becomes the value assigned plus the number of seconds since the assignment.

This means you can simply query this variable directly at the end of your script to print the elapsed time:

#!/usr/bin/env bash

# Do stuff...

echo "Script finished in $SECONDS seconds."

You can also time smaller sections like so:

#!/usr/bin/env bash

# Do stuff

SECONDS=0

# Do timed stuff...

echo "Timed stuff finished in $SECONDS seconds."
薆情海 2024-10-26 17:15:16

这是查找经过的时间(以毫秒为单位)的脚本。将 sleep 60 行替换为您要执行的代码。

a=0
while [ $a -lt 10 ]
do
    START_TIME=`echo $(($(date +%s%N)/1000000))`
    sleep 3
    END_TIME=`echo $(($(date +%s%N)/1000000))`
    ELAPSED_TIME=$(($END_TIME - $START_TIME))
    echo $ELAPSED_TIME
    if [ $a -eq 10 ]
    then
        break
    fi
    a=`expr $a + 1`
done

Here is the script to find the time elapsed in milliseconds. Replace the sleep 60 line with the code you want to execute.

a=0
while [ $a -lt 10 ]
do
    START_TIME=`echo $(($(date +%s%N)/1000000))`
    sleep 3
    END_TIME=`echo $(($(date +%s%N)/1000000))`
    ELAPSED_TIME=$(($END_TIME - $START_TIME))
    echo $ELAPSED_TIME
    if [ $a -eq 10 ]
    then
        break
    fi
    a=`expr $a + 1`
done
沒落の蓅哖 2024-10-26 17:15:16

GNU time

我也对 GNU time 命令非常感兴趣:https://www.gnu.org/software/time/ 与 Bash 内置的 time 相比,它提供了一些重要的选项 -在。

示例用法:

env time --format '%e' --output time.log sleep 1

输出:

1.00

解释:

  • env:查找 /usr/bin/time 而不是 Bash 内置的

  • --format '%e':以秒为单位打印时间,请参阅人工时间

    这通常是我在基准测试时想要的:单个数字而不是分钟 + 秒。

我经常使用的一个重要模式是:

bench-cmd() (
  logfile=time.log
  echo "cmd $@" >> "$logfile"
  printf 'time ' >> "$logfile"
  bench_cmd="env time --append --format '%e' --output '$logfile' $@"
  eval "$bench_cmd"
  echo >> "$logfile"
)

rm -f time.log
bench-cmd sleep 1
bench-cmd sleep 2
bench-cmd sleep 3
cat time.log

GitHub 上游

输出:

cmd sleep 1
time 1.00

cmd sleep 2
time 2.00

cmd sleep 3
time 3.00

说明:

  • --output:将时间输出到文件。

    默认情况下,输出会发送到 stderr,因此此选项对于将时间与命令的 stderr 分开非常重要。

  • --append:附加到文件而不是覆盖。

    这使我可以将整个基准测试输出集中在一个文件中。

GNU time

I'm also a big fun of the GNU time command: https://www.gnu.org/software/time/ which offers some important options compared to the time Bash built-in.

Sample usage:

env time --format '%e' --output time.log sleep 1

Output:

1.00

Explanation:

  • env: to find /usr/bin/time instead of the Bash built-in

  • --format '%e': print time in seconds, see man time.

    This is often what I want when benchmarking: a single number rather than minutes + seconds.

And an important pattern I often use is:

bench-cmd() (
  logfile=time.log
  echo "cmd $@" >> "$logfile"
  printf 'time ' >> "$logfile"
  bench_cmd="env time --append --format '%e' --output '$logfile' $@"
  eval "$bench_cmd"
  echo >> "$logfile"
)

rm -f time.log
bench-cmd sleep 1
bench-cmd sleep 2
bench-cmd sleep 3
cat time.log

GitHub upstream.

Output:

cmd sleep 1
time 1.00

cmd sleep 2
time 2.00

cmd sleep 3
time 3.00

Explanation:

  • --output: output the time to a file.

    By default, the output goes to stderr, so this option is important to separate the timing from the stderr of the command.

  • --append: append to the file instead of overwriting.

    This allows me to concentrate the entire benchmark output in a single file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文