linux程序执行时长,linux - 在sh中获取程序执行时间

linux - 在sh中获取程序执行时间

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

我知道我可以编写一个可以执行此操作的perl或python脚本,但有没有办法在shell中执行此操作? (恰好是bash)

ʞɔıu asked 2019-01-28T02:48:51Z

9个解决方案

477 votes

使用内置的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

Robert Gamble answered 2019-01-28T02:49:02Z

111 votes

您可以使用时间(1)获得比bash内置的time(Robert Gamble提到的)更详细的信息。 通常这是time。

编者注:要确保您调用外部实用程序time而不是shell的time关键字,请将其调用为/usr/bin/time。

time是POSIX强制实用程序,但它需要支持的唯一选项是-p。

特定平台实现特定的非标准扩展:-v与GNU的time实用程序一起使用,如下所示(问题标记为linux); BSD / macOS实现使用-l来产生类似的输出 - 见man 1 time。

详细输出示例:

grepsedawk answered 2019-01-28T02:49:58Z

72 votes

#!/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"

David answered 2019-01-28T02:50:14Z

34 votes

对于逐行delta测量,请尝试gnomon。

命令行实用程序,有点像moreutils的ts,用于将时间戳信息添加到另一个命令的标准输出。 对于长期运行的过程非常有用,在这些过程中,您需要记录长时间运行的历史记录。

您还可以使用--high和/或--medium选项指定以秒为单位的长度阈值,gnomon将以红色或黄色突出显示时间戳。 你也可以做其他一些事情。

lhuCw.gif

Adriano Resende answered 2019-01-28T02:50:53Z

16 votes

如果您想要更高的精度,请使用%N和date(并使用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

Benoit Duffez answered 2019-01-28T02:51:30Z

13 votes

如果您打算稍后使用计算时间,请了解如何使用/usr/bin/time的$timefile选项输出可节省时间的代码。 这里是我最近使用的一些代码,用于获取和整理整个学生课程的执行时间:

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文件,并将输出传输到Lua解释器。 您可以使用Python或bash或任何您喜欢的语法执行相同的操作。 我喜欢这种技巧。

Norman Ramsey answered 2019-01-28T02:52:02Z

10 votes

如果您只需要精度到秒,您可以使用内置$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

glenn jackman answered 2019-01-28T02:52:26Z

6 votes

您可以使用{}和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

}

Eduardo Cuomo answered 2019-01-28T02:52:55Z

1 votes

方式是

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

$ > time ./per

输出是&gt;&gt;

real 0m0.014s

user 0m0.010s

sys 0m0.002s

Robel Sharma answered 2019-01-28T02:53:26Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值