AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 1515767
Accepted
dmkonlinux
dmkonlinux
Asked: 2024-05-29 21:43:52 +0800 CST2024-05-29 21:43:52 +0800 CST 2024-05-29 21:43:52 +0800 CST

我应该使用“time”关键字、“/usr/bin/time”命令还是内置的“times”来计时 bash 中命令的执行时间?

  • 772

我第一次深入研究 bash,并想尝试测试一些代码来比较实现相同目标的不同方法。作为其中的一部分,我遇到了记录代码运行时间的 time 和 times 方法。对于通过以下方式创建的简单的一行脚本:

echo "echo a script" > script

我得到:

$ time bash ./script
a script

real    0m0.009s
user    0m0.006s
sys 0m0.002s

$ times bash ./script
0m0.082s 0m0.074s
0m0.019s 0m0.041s

$ /usr/bin/time bash ./script
a script
0.00user 0.00system 0:00.00elapsed 80%CPU (0avgtext+0avgdata 3328maxresident)k
0inputs+0outputs (0major+148minor)pagefaults 0swaps
$ type -a time
time is a shell keyword
time is /usr/bin/time
time is /bin/time
$ type -a times
times is a shell builtin

$ which -a time
/usr/bin/time
/bin/time

bash -c help带我去:

$ help time
time: time [-p] pipeline  
    Report time consumed by pipeline's execution.

$ help times
times: times  
    Display process times.

并man time给我:

TIME(1)........通用命令手册.........TIME(1)

NAME time - 运行程序并总结系统资源使用情况

bash shell 的用户需要使用显式路径才能运行外部时间命令,而不是 shell 内置变体。在时间安装在 /usr/bin 中的系统上,第一个示例将变为/usr/bin/time wc /etc/hosts

bash 参考手册将其描述time为保留字,“对 shell 有特殊含义。它们用于开始和结束 shell 的复合命令”,而将times其描述为shell 内置命令,“继承自 Bourne Shell。这些命令按照 POSIX 标准的规定来实现”。

除了手册中的一句话:

使用时间作为保留字允许对 shell 内置函数、shell 函数和管道进行计时。外部时间命令无法轻松地对这些进行计时。

我看不出三个非常相似的命令的原因。

我应该使用哪一种来测试不同的方法并优化我的脚本?

command-line
  • 1 1 个回答
  • 537 Views

1 个回答

  • Voted
  1. Best Answer
    kos
    2024-05-29T22:43:54+08:002024-05-29T22:43:54+08:00

    times(内置)

    times- 按照help times:

    打印 shell 及其所有子进程的累积用户和系统时间。

    强调“累积”和“对于 shell 及其所有子进程”。它不带任何参数,也不测量另一个命令的执行时间:它打印 shell 及其子级执行所花费的当前用户和系统时间。

    例如,如果我想要printf空字符串一百万次,那么 shell 运行所有这些命令所需的时间将非常重要(重点关注注释行的第一个字段):

    > times; for x in {1..1000000}; do printf ''; done; times
    # running printf a million times in the current shell
    0m0,021s 0m0,000s # shell's user time is about 0
    0m0,009s 0m0,003s
    0m2,232s 0m0,087s # shell's user time increased by about 2 seconds
    0m0,009s 0m0,003s
    > times; (for x in {1..1000000}; do printf ''; done); times
    # running printf a million times in a subshell
    0m2,239s 0m0,090s
    0m0,009s 0m0,009s # shell's children user time is about 0
    0m2,239s 0m0,096s
    0m2,308s 0m0,153s # shell's children user time increased by about 2 seconds
    

    time这赋予了它与(保留字)和(可执行文件)完全不同的用途/usr/bin/time。大多数时候(不是双关语),在脚本中,您将使用time和/usr/bin/time。

    time(保留字)/ /usr/bin/time(可执行文件)

    正如 @terdon 关键指出的那样,time以“可以通过 exec 函数系列访问”的方式实现的“实用程序”必须存在于符合 POSIX 标准的系统上1;仅此一点就证明了/usr/bin/timeexcept的存在time。

    保留字和可执行文件之间最显着的区别是:

    • 第一个是空壳设施。这意味着您始终能够在 Bash 中使用它(我猜在大多数 shell 中),而您不一定能够在不兼容 POSIX 的系统上使用第二个;相反,如果您运行的 shell 没有第一个,则根据系统及其 POSIX 合规性,您仍然可以使用第二个。实际上,我个人从未使用过没有 Bash 或/usr/bin/time.另请注意,一般来说,内置可执行文件的运行速度通常比外部可执行文件快。
    • 第一个可以应用于内置函数、函数、列表和子 shell;第二个可能不会。
    time printf ''
    function foo() {}; time foo
    time { foo; bar; }
    time (foo)
    

    所以在实践中,至少在Ubuntu上,就单个命令而言,两者之间的区别主要归结为打印输出的格式。

    使用保留字,您几乎没有可用的输出定制(基本上您可以使用默认格式和 POSIX 兼容格式进行打印),而使用可执行文件,您有更多的可用选项(请参阅 参考资料man time)。

    > time sleep 5; printf '\n'; /usr/bin/time sleep 5
    
    real    0m5,002s
    user    0m0,001s
    sys     0m0,000s
    
    0.00user 0.00system 0:05.00elapsed 0%CPU (0avgtext+0avgdata 2176maxresident)k
    0inputs+0outputs (0major+95minor)pagefaults 0swaps
    

    1. 从链接:

    任何标准实用程序都可以作为命令语言解释器内的常规内置实用程序来实现。这样做通常是为了提高常用实用程序的性能或实现在单独环境中更困难的功能。 [...]但是,所有标准实用程序,包括表中的常规内置实用程序,但不包括特殊内置实用程序中描述的特殊内置实用程序,都应以某种方式实现,以便可以访问它们通过 POSIX.1-2008 系统接口卷中定义的 exec 函数系列,可以由需要它的标准实用程序(env、find、nice、nohup、time、xargs)直接调用。

    • 7

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve