linux - 使用linux命令获取以秒为单位的挂钟时间?

标签 linux time

如何使用 Linux 命令来获取执行程序所花费的时间(以秒为单位)。在下面的例子中,我期望得到“0.005”。

$ time ls >/dev/null

real    0m0.005s
user    0m0.001s
sys 0m0.003s

最佳答案

取决于您的路径:

/usr/bin/time -f "%e"

正常时间由 bash 的(如果你碰巧使用 bash)intern time 命令给出

type time

当你需要的时候,

which time

会发现。

因此,在您的命令上下文中:

/usr/bin/time -f "%e" ls > /dev/null

但是要将其存储在变量中,您不能使用

a=$(/usr/bin/time -f "%e" ls > /dev/null)

因为时间的输出被写入错误流,所以不会对程序输出造成影响(在本例中ls)。有关更多详细信息,请参阅 time 的联机帮助页。

调用程序的另一种方法是在其前面添加关键字 command:

command time -f "%e" ls 

关于linux - 使用linux命令获取以秒为单位的挂钟时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49080949/

相关文章:

c++ - 编译错误 : linux/module. h: 没有这样的文件或目录

linux - OSX sed : how to use the escape character in the second field of a `s` operation?

php - 在 PHP 中添加时间

bash - 在 Bash 中将秒数添加到当前时间

android - 比较字符串时间和当前时间android

javascript - jQuery 中的表单输入类型时间清除事件

c - ARM 程序集 - 为什么我的应用程序在将 r7 归零时崩溃?

linux - GNU ld 的 --audit 标志有什么作用?

python - 根据 GMT(伦敦)夏令时检测和更改时间增量?

linux - bash脚本错误