使用/ usr / bin / time Shell命令提高%e精度


19

当我在shell中运行time命令时,time ./myapp我得到如下输出:

real    0m0.668s
user    0m0.112s
sys     0m0.028s

但是,当我运行命令时,\time -f %e ./myapp我失去了精度,并且得到:

2.01s

如果我使用该%E命令,我也会以相同的方式失去精度。我如何更改它以再次获得更高的精度,但仍然只输出秒数?

我的研究基于以下Linux / Unix命令:时间这个问题

Answers:


21

我假设您了解这两个命令都在调用不同版本的时间,对吗?

bash的内置版本

% time

GNU时间又名。/ usr / bin /时间

% \time

可以在此处阅读内置time命令bash

% 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.

GNU time/usr/bin/time通常比内置的要有用。

关于您的精度问题,请参见github gist中的内容

为什么bash时间比GNU时间更精确?

内置的bash命令时间提供了毫秒级的执行精度,而GNU时间(通常是/ usr / bin / time)提供了毫秒级的精度。times(2)系统调用以时钟为单位给出时间,而100个时钟通常为1秒,因此精度类似于GNU时间。什么是bash时间以便更精确?

Bash时间在内部使用getrusage(),而GNU时间在使用times()。由于微秒的分辨率,getrusage()更加精确。

您可以通过以下示例看到厘秒(请参见输出的第5行):

% /usr/bin/time -v sleep .22222
    Command being timed: "sleep .22222"
    User time (seconds): 0.00
    System time (seconds): 0.00
    Percent of CPU this job got: 0%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.22
    Average shared text size (kbytes): 0
    Average unshared data size (kbytes): 0
    Average stack size (kbytes): 0
    Average total size (kbytes): 0
    Maximum resident set size (kbytes): 1968
    Average resident set size (kbytes): 0
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 153
    Voluntary context switches: 2
    Involuntary context switches: 1
    Swaps: 0
    File system inputs: 0
    File system outputs: 0
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0

可以使用bash这样的time命令获得更高的分辨率,并且您可以控制分辨率:

# 3 places 
% TIMEFORMAT='%3R'; time ( sleep .22222 )
0.224

Bash手册中的变量

TIMEFORMAT
The value of this parameter is used as a format string specifying how the timing information for pipelines prefixed with the time reserved word should be displayed. The ‘%’ character introduces an escape sequence that is expanded to a time value or other information. The escape sequences and their meanings are as follows; the braces denote optional portions.

%%
A literal ‘%’.

%[p][l]R
The elapsed time in seconds.

%[p][l]U
The number of CPU seconds spent in user mode.

%[p][l]S
The number of CPU seconds spent in system mode.

%P
The CPU percentage, computed as (%U + %S) / %R.

The optional p is a digit specifying the precision, the number of fractional digits after a decimal point. A value of 0 causes no decimal point or fraction to be output. At most three places after the decimal point may be specified; values of p greater than 3 are changed to 3. If p is not specified, the value 3 is used.

The optional l specifies a longer format, including minutes, of the form MMmSS.FFs. The value of p determines whether or not the fraction is included.

If this variable is not set, Bash acts as if it had the value

$'\nreal\t%3lR\nuser\t%3lU\nsys\t%3lS'
If the value is null, no timing information is displayed. A trailing newline is added when the format string is displayed.

是的,我已经知道所有这些。所以我可以得出结论,我想要的是不可能的?有没有一种方法可以使用bash时间版本,并且仅以3位数字获得真实秒的时间?
Flame_Phoenix

查看我的更新,您可以使用变量TIMEFORMAT控制bash的time命令。那是您要找的东西吗?
slm

是的,就是这样,谢谢!不过,我想知道如何将其附加到文件中?我正在尝试使用%TIMEFORMAT ='%3R'; 时间(sleep .22222)>> bla.txt,但它不起作用:S无论如何,已选择。我希望我能给您业力++,但是我没有15业力
-.

2
TIMEFORMAT ='%3R'; 时间(sleep .22222)2 >> myFile.txt知道了!
Flame_Phoenix

两者的长期用户并且都不知道TIMEFORMAT,我一直使用sed提取实时数据。谢谢!“由于微秒的分辨率”。语句提出了希望,TIMEFORMAT=%6R但可能会出现,但精度数字可能仅为0-3。
mxmlnkn
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.