bash /usr/bin/time --format 输出经过的时间(以毫秒为单位)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16959337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:48:24  来源:igfitidea点击:

/usr/bin/time --format output elapsed time in milliseconds

bashtimedebiantime-format

提问by Preexo

I use the /usr/bin/time program to measure the time for a command. with the --format parameter i can format the output. e.g.

我使用 /usr/bin/time 程序来测量命令的时间。使用 --format 参数,我可以格式化输出。例如

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

is there a way to output a bigger accuracy of the elapsed seconds? or just output milliseconds, not seconds?

有没有办法输出经过秒数的更大精度?还是只输出毫秒,而不是秒?

In the manual of /usr/bin/time it only says something about seconds, but maybe there is a way and someone can help me... thanks!

在 /usr/bin/time 的手册中,它只说了几秒钟,但也许有办法,有人可以帮助我……谢谢!

EDIT: I know about the bash command "time" which uses the format of the environment variable "TIMEFORMAT". sorry, but i don't wanna change that env-var... seems to risky to me, solution should be something that doesn't change the running system at all :)

编辑:我知道使用环境变量“TIMEFORMAT”格式的 bash 命令“time”。对不起,但我不想改变那个 env-var ......对我来说似乎有风险,解决方案应该是根本不会改变正在运行的系统的东西:)

回答by devnull

One possibility is to use the datecommand:

一种可能性是使用date命令:

ts=$(date +%s%N) ; my_command ; tt=$((($(date +%s%N) - $ts)/1000000)) ; echo "Time taken: $tt milliseconds"

%Nshould return nanoseconds, and 1 millisecond is 1000000 nanosecond, hence by division would return the time taken to execute my_commandin milliseconds.

%N应该返回nanoseconds,1 毫秒是 1000000 纳秒,因此除法将返回my_command以毫秒为单位执行的时间。

NOTEthat the %Nis not supported on all systems, but most of them.

请注意%N并非所有系统都支持 ,但大多数系统都支持。

回答by Infineight

For convenience I made devnull's answer into a script (I named it millisecond-time).

为方便起见,我将 devnull 的答案制作成了一个脚本(我将其命名为毫秒时间)。

#!/bin/bash
ts=$(date +%s%N) ; $@ ; tt=$((($(date +%s%N) - $ts)/1000000)) ; echo "Time taken: $tt milliseconds"

I put the script in /usr/local/bin.
Gave it execute rights chmod +x /usr/local/bin/millisecond-time.
Now I can use it like this: millisecond-time my_command

我把脚本放在/usr/local/bin.
给它执行权限chmod +x /usr/local/bin/millisecond-time
现在我可以这样使用它:millisecond-time my_command

P.s. This would be a comment if I had the rep'.

Ps 如果我有代表,这将是一个评论。