我想在几种不同条件下在linux shell中执行某些操作,并能够输出每次执行的执行时间。
我知道我可以编写一个可以执行此操作的perl或python脚本,但是有什么方法可以在shell中执行它吗? (碰巧是bash)
#1楼
#!/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"
#2楼
如果需要更高的精度,请在date
使用%N
(对于差异使用bc
,因为$(())
仅处理整数)。
方法如下:
start=$(date +%s.%N)
# do some stuff here
dur=$(echo "$(date +%s.%N) - $start" | bc)
printf "Execution time: %.6f seconds" $dur
例:
start=$(date +%s.%N); \
sleep 0.1s; \
dur=$(echo "$(date +%s.%N) - $start" | bc); \
printf "Execution time: %.6f seconds\n" $dur
结果:
Execution time: 0.104623 seconds
#3楼
使用内置的time
关键字:
$ help time time: time [-p] PIPELINE Execute PIPELINE and print a summary of the real time, user CPU time, and system CPU time spent executing PIPELINE when it terminates. The return status is the return status of PIPELINE. The `-p' option prints the timing summary in a slightly different format. This uses the value of the TIMEFORMAT variable as the output format.
例:
$ time sleep 2
real 0m2.009s user 0m0.000s sys 0m0.004s
#4楼
您可以使用time(1)获得比bash内置time
(Robert Gamble提到的时间 )更为详细的信息。 通常这是/usr/bin/time
。
编者注:为了确保您调用的是外部实用程序 time
而不是shell的time
关键字 ,请以/usr/bin/time
调用它。time
是POSIX强制的实用程序 ,但支持-p
是唯一需要的选项。
特定的平台实现特定的非标准扩展: -v
与GNU的time
实用程序一起使用,如下所示(问题标记为linux );BSD / macOS实现使用-l
产生类似的输出-参见man 1 time
。
详细输出示例:
$ /usr/bin/time -v sleep 1
Command being timed: "sleep 1"
User time (seconds): 0.00
System time (seconds): 0.00
Percent of CPU this job got: 1%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.05
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 0
Average stack size (kbytes): 0
Average total size (kbytes): 0
Maximum resident set size (kbytes): 0
Average resident set size (kbytes): 0
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 210
Voluntary context switches: 2
Involuntary context switches: 1
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 0
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
#5楼
如果打算稍后使用时间进行计算,请学习如何使用/usr/bin/time
的-f
选项输出节省时间的代码 。 这是我最近用来获取和分类整个学生程序的执行时间的一些代码:
fmt="run { date = '$(date)', user = '$who', test = '$test', host = '$(hostname)', times = { user = %U, system = %S, elapsed = %e } }"
/usr/bin/time -f "$fmt" -o $timefile command args...
后来我连接了所有$timefile
文件,并将输出通过管道$timefile
到Lua解释器中 。 您可以使用Python或bash或您喜欢的语法进行相同的操作。 我喜欢这种技术。
#6楼
对于逐行增量测量,请尝试gnomon 。
一个命令行实用程序,有点像moreutils的ts,用于将时间戳信息放在另一个命令的标准输出之前。 对于长时间运行的过程很有用,在该过程中您需要花费很长时间的历史记录。
您还可以使用--high
和/或--medium
选项指定以秒为单位的长度阈值,超过该阈值,gnomon将以红色或黄色突出显示时间戳。 您还可以做其他一些事情。
#7楼
您可以使用time
和subshell ()
:
time (
for (( i=1; i<10000; i++ )); do
echo 1 >/dev/null
done
)
或在同一外壳{}
:
time {
for (( i=1; i<10000; i++ )); do
echo 1 >/dev/null
done
}
#8楼
如果只需要秒精度,则可以使用内置的$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
#9楼
一种可能的简单方法(可能无法满足不同用户的需求)是使用Shell PROMPT。它是一种简单的解决方案,在某些情况下可能会有用。 您可以使用bash提示功能,如下例所示:
export PS1='[\t \u@\h]\$'
上面的命令将导致将shell提示符更改为:
[HH:MM:SS username@hostname]$
每次您运行命令(或按Enter键)返回到shell提示时,该提示都会显示当前时间。
笔记:
1)请注意,如果您在键入下一条命令之前等待了一段时间,则需要考虑该时间,即,shell提示符中显示的时间是显示shell提示符时的时间戳,而不是您输入命令时的时间戳。 一些用户选择按Enter键以获取具有新时间戳记的新提示,然后再准备下一条命令。
2)还有其他可用的选项和修饰符可用于更改bash提示,有关更多详细信息,请参考(man bash)。
#10楼
方式是
$ > g++ -lpthread perform.c -o per
$ > time ./per
输出是>>
real 0m0.014s
user 0m0.010s
sys 0m0.002s