如何在Linux shell脚本中以秒为单位度量持续时间?

时间:2022-03-11 07:15:34

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

我希望了解一个Linux shell脚本的操作需要多长时间。我该怎么做呢?

6 个解决方案

#1


81  

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

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

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:

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

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.

我认为这是针对bash的,但是由于您使用的是Linux,所以我假设您使用的是bash。

#2


34  

Use the time command. time ls /bin.

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

#3


25  

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

#4


5  

You can use the "time" command. Just prepend "time" before the command you want to measure the duration of. (Source: http://unixhelp.ed.ac.uk/CGI/man-cgi?time )

您可以使用“time”命令。只需在您想要度量的命令的持续时间之前加上“time”。(来源:http://unixhelp.ed.ac.uk/CGI/man-cgi?时间)

#5


3  

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

下面的脚本以毫秒为单位查找运行时间。用要执行的代码替换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

#6


1  

Just to help anyone like me that receive an error:

只是为了帮助像我这样受到错误的人:

 arithmetic expression: expecting primary: "-"

Check your shellscript that shall start with:

请检查您的shell脚本,该脚本应以以下内容开始:

#!/bin/bash

Cheers!

干杯!

#1


81  

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

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

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:

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

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.

我认为这是针对bash的,但是由于您使用的是Linux,所以我假设您使用的是bash。

#2


34  

Use the time command. time ls /bin.

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

#3


25  

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

#4


5  

You can use the "time" command. Just prepend "time" before the command you want to measure the duration of. (Source: http://unixhelp.ed.ac.uk/CGI/man-cgi?time )

您可以使用“time”命令。只需在您想要度量的命令的持续时间之前加上“time”。(来源:http://unixhelp.ed.ac.uk/CGI/man-cgi?时间)

#5


3  

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

下面的脚本以毫秒为单位查找运行时间。用要执行的代码替换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

#6


1  

Just to help anyone like me that receive an error:

只是为了帮助像我这样受到错误的人:

 arithmetic expression: expecting primary: "-"

Check your shellscript that shall start with:

请检查您的shell脚本,该脚本应以以下内容开始:

#!/bin/bash

Cheers!

干杯!