From: Coelacanthus Date: Fri, 13 Nov 2020 09:13:57 +0000 (+0800) Subject: feat(cmd.md): add built-in time && GNU time X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=86030ed472f4234868099b26aa779f3bf69053ba;p=oi-wiki%2Fmain.git feat(cmd.md): add built-in time && GNU time --- diff --git a/docs/tools/cmd.md b/docs/tools/cmd.md index 35726680..6fa408be 100644 --- a/docs/tools/cmd.md +++ b/docs/tools/cmd.md @@ -151,6 +151,16 @@ command < input > output 此外,在 Unix-like 系统上,还有可通过 `man command` 获取的“手册”(manual),相比“帮助”一般更为详细。 +### built-in time 和 GNU time + +对于测试程序运行时间,我们通常可以使用 `time` 命令。 + +但是这个命令实际上在系统中有两个对应的命令:一个是部分 Shell(例如 Bash)内建的命令,一个是 GNU time(是一个单独的软件)。这两个之间存在一些差异。 + +一般在 Bash 中直接使用 `time` 调用的是 Bash 内建的版本,我们可以使用 `TIMEFORMAT` 环境变量控制其输出格式,例如将其设为 `%3lR` 即可输出三位精度的实际运行时间,`%3lU` 即可输出三位精度的用户空间运行时间。[^bash-time-format] + +如果想要调用 GNU 版本的 time,则需使用 `\time` 或者 `/usr/bin/time` 调用,但是他的输出格式并不易读,我们可以附加 `-p` 参数(即为 `\time -p`)来获得易读的输出。 + ## 参考资料与注释 [^1]: 刘汝佳《算法竞赛入门经典(第 2 版)》附录 A 开发环境与方法 @@ -158,3 +168,5 @@ command < input > output [^have-to-link-libm-in-gcc]: [Why do you have to link the math library in C?](https://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c) [^autocomplete]: [Comparison_of_command_shells#Interactive_features](https://en.wikipedia.org/wiki/Comparison_of_command_shells#Interactive_features) + +[^bash-time-format]: [https://unix.stackexchange.com/a/70655](https://unix.stackexchange.com/a/70655)