Perl高精度时间模块-Time::HiRes
2015-09-26 07:48:35 阿炯

Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers(高精度的alarm,sleep,gettimeofday,间隔定时器)。简言之,我们之前在程序中休眠(sleep函数为perl内置)的话,只能是整数的秒,如果想休0.1s的话,原生内置的sleep函数是无法实现的,而Time::HiRes包的slepp函数是可以的。该模块也是perl内置的模块,默认不导入任何函数,一旦导入,原来的内置同名函数将失效。


The Time::HiRes module implements a Perl interface to the usleep , nanosleep , ualarm , gettimeofday , and setitimer /getitimer system calls, in other words, high resolution time and timers. See the EXAMPLES section below and the test scripts for usage; see your system documentation for the description of the underlying nanosleep or usleep , ualarm , gettimeofday , and setitimer /getitimer calls.

Time::HiRes模块实现了一个Perl接口usleep nanosleep,ualarm,gettimeofday,和setitimer/getitimer系统调用,换句话,即高精度的时间和计时器。请参阅示例部分并在脚本中测试,参考你的系统文档描述文档:nanosleep或usleep ualarm,gettimeofday,setitimer/getitimer调用。

If your system lacks gettimeofday() or an emulation of it you don't get gettimeofday() or the one-argument form of tv_interval() . If your system lacks all of nanosleep() , usleep() , select(), and poll , you don't get Time::HiRes::usleep() , Time::HiRes::nanosleep() , or Time::HiRes::sleep() . If your system lacks both ualarm() and setitimer() you don't get Time::HiRes::ualarm() or Time::HiRes::alarm() .

如果系统没有gettimeofday()函数,或一个gettimeofday()模拟函数,或一个带参数形式的tv_interval()都没有的话。如果您的系统缺乏所有nanosleep(),usleep(),select(),poll,你没有得到Time::HiRes::usleep(),Time::HiRes::nanosleep()或Time::HiRes::sleep()。如果您的系统没有Time::HiRes::ualarm()或Time::HiRes::alarm()就不会有ualarm()和setitimer()函数。

If you try to import an unimplemented function in the use statement it will fail at compile time.

如果想导入一个未实现的函数在编译时则该语句将失败。

If your subsecond sleeping is implemented with nanosleep() instead of usleep() , you can mix subsecond sleeping with signals since nanosleep() does not use signals. This, however, is not portable, and you should first check for the truth value of &Time::HiRes::d_nanosleep to see whether you have nanosleep, and then carefully read your nanosleep() C API documentation for any peculiarities.

如果你的次秒级实现睡眠使用的是nanosleep()函数而不是usleep(),可以用信号结合次秒级休眠,因为nanosleep()不使用信号。注意:这是不可移植的,你应该首先检查相关的时间函数&Time::HiRes::d_nanosleep nanosleep是否真的存在,否则就应该仔细阅读你nanosleep()的C API文档。

If you are using nanosleep for something else than mixing sleeping with signals, give some thought to whether Perl is the tool you should be using for work requiring nanosecond accuracies.

如果使用的是nanosleep而不是结合与其他信号的休眠,让一些人认为Perl是否应该使用,其是否能要求纳秒级的精度。

Remember that unless you are working on a hard realtime system, any clocks and timers will be imprecise, especially so if you are working in a pre-emptive multiuser system. Understand the difference between wallclock time and process time (in UNIX-like systems the sum of user and system times). Any attempt to sleep for X seconds will most probably end up sleeping more than that, but don't be surprised if you end up sleeping slightly less.

注意:除非你是在做一个高难度的实时系统,任何时钟和计时器都不是精确的,特别是工作在一个任务抢占式的多用户系统中。理解wallclock时间和处理时间之间的区别(在类unix系统用户和系统时间的总和)。任何试图睡X秒将最有可能最终休眠的更多,不要奇怪,如果你最终略少休眠的话。

The following functions can be imported from this module. No functions are exported by default.

以下功能可以从这个模块导入,默认情况下没有导出任何函数。

gettimeofday ()
In array context returns a two-element array with the seconds and microseconds since the epoch. In scalar context returns floating seconds like Time::HiRes::time() (see below).

返回数组双元素,数组上下文分别为秒和毫秒,在标量上下文返回浮动数。

usleep ($useconds)
Sleeps for the number of microseconds (millionths of a second) specified. Returns the number of microseconds actually slept. Can sleep for more than one second, unlike the usleep system call. Can also sleep for zero seconds, which often works like a thread yield.

以微秒为单位指定休眠的时间,返回的数量实际上的休眠微秒。与原系统中sleep()不同(最起码一秒),而usleep系统调用0秒也可以休眠,经常线程一起使用。

nanosleep ( $nanoseconds )
Sleeps for the number of nanoseconds (1e9ths of a second) specified. Returns the number of nanoseconds actually slept (accurate only to microseconds, the nearest thousand of them). Can sleep for more than one second. Can also sleep for zero seconds, which often works like a thread yield.

以纳秒(1e9ths 秒)为休眠单位,返回实际睡的纳秒数(准确到微秒,最近的千)。与原系统中sleep()不同(最起码一秒),而usleep系统调用0秒也可以休眠,经常线程一起使用。

ualarm ( $useconds [, $interval_useconds ] )

Issues a ualarm call; the $interval_useconds is optional and will be zero if unspecified, resulting in alarm-like behaviour.
Returns the remaining time in the alarm in microseconds, or undef if an error occurred.
ualarm(0) will cancel an outstanding ualarm().

调用ualarm函数时,interval_useconds是可选,不指定将为零,会导致alarm-like行为。
调用ualarm将返回的剩余时间,以微秒为单,如果发生错误或undef。
ualarm(0)将取消之前的ualarm()。

tv_interval
tv_interval ( $ref_to_gettimeofday [, $ref_to_later_gettimeofday] )
Returns the floating seconds between the two times, which should have been returned by gettimeofday() . If the second argument is omitted, then the current time is used.

返回两次调用间的浮动秒,由gettimeofday()函数调用时返回的。如果省略第二个参数,则使用当前时间。

time ()
Returns a floating seconds since the epoch. This function can be imported, resulting in a nice drop-in replacement for the time provided with core Perl.

返回一个自己Epoch以来的浮动秒数(Epoch指的是一个特定的时间:1970-01-01 00:00:00 UTC)。当这个函数被导入时,将替代核心Perl中所提供的原生time()函数。

sleep ($floating_seconds)
Sleeps for the specified amount of seconds. Returns the number of seconds actually slept (a floating point value). This function can be imported, resulting in a nice drop-in replacement for the sleep provided with perl.

指定休眠的秒数,直接调用时返回实际睡的秒数(一个浮点值)。当此函数被导入时,将替代核心Perl中所提供的原生sleep()函数。

alarm ($floating_seconds [, $interval_floating_seconds])
The SIGALRM signal is sent after the specified number of seconds. Implemented using setitimer() if available, ualarm() if not. The $interval_floating_seconds argument is optional and will be zero if unspecified, resulting in alarm()-like behaviour. This function can be imported, resulting in a nice drop-in replacement for the alarm provided with perl.

在指定的秒数后发送SIGALRM信号。使用setitimer()来实现如果可用的话,ualarm()则不是。$interval_floating_seconds参数是可选的,如果不指定则为零,会导致之前的alarm()函数被取消。当此函数被导入时,将替代核心Perl中所提供的原生alarm()函数。

setitimer ($which, $floating_seconds [, $interval_floating_seconds ])
Start up an interval timer: after a certain time, a signal ($which) arrives, and more signals may keep arriving at certain intervals. To disable an "itimer", use $floating_seconds of zero. If the $interval_floating_seconds is set to zero (or unspecified), the timer is disabled after the next delivered signal.

启动一个间隔计时器:一段时间后,一个信号($which)到,和更多的信号可能在一定的时间间隔到达。当$floating_seconds为0时,将禁用一个“itimer”。如果$ interval_floating_seconds设置为0(或指定),计时器在下次发送信号将会禁用。

getitimer ($which)
Return the remaining time in the interval timer specified by $which.
In scalar context, the remaining time is returned.
In list context, both the remaining time and the interval are returned. The interval is always what you put in using setitimer().

返回指定的间隔计时器$which的剩余时间。
在标量上下文中返回剩余时间。
在列表的环境下,返回剩余时间和返回时间间隔,时间总是所的使用setitimer()。

clock_gettime ($which)
Return as seconds the current value of the POSIX high resolution timer specified by $which.

返回当前值由$which指定秒POSIX高分辨率定时器值,单位为秒。

clock_getres ($which)
Return as seconds the resolution of the POSIX high resolution timer specified by $which . All implementations that support POSIX high resolution timers are supposed to support at least the $which value of CLOCK_REALTIME , see clock_gettime.

返回秒POSIX的分辨率高分辨率定时器规定的秒。所有实现都支持POSIX高分辨率计时器应该支持至少CLOCK_REALTIME,参见clock_gettime。

clock_nanosleep ( $which, $nanoseconds, $flags = 0)
Sleeps for the number of nanoseconds (1e9ths of a second) specified. Returns the number of nanoseconds actually slept.

以纳秒为单位指定休眠的(1e9ths秒)时间,返回实际所用的休眠,以纳秒为单位。

clock()
Return as seconds the process time (user + system time) spent by the process since the first call to clock() (the definition is not "since the start of the process", though if you are lucky these times may be quite close to each other, depending on the system). What this means is that you probably need to store the result of your first call to clock(), and subtract that value from the following results of clock().

The time returned also includes the process times of the terminated child processes for which wait() has been executed. This value is somewhat like the second value returned by the times() of core Perl, but not necessarily identical. Note that due to backward compatibility limitations the returned value may wrap around at about 2147 seconds or at about 36 minutes.



(l)stat
stat FH|EXPR
As stat or lstat but with the access/modify/change file timestamps in subsecond resolution, if the operating system and the filesystem both support such timestamps. To override the standard stat().

stat或lstat函数用于得到文件的访问/修改/改变文件时间戳,次秒级单位,如果操作系统和文件系统都支持这样的时间戳的话。覆盖标准内置的stat()函数。

示例

use Time::HiRes qw(sleep time);
$| = 1;
my $before = time;
for my $i (1..100)
{
 print "$i\n";
 sleep(0.01);
}
printf("time used : %.5f seconds\n", time - $before);

$| 输出缓冲区开关,默认为0,即关闭
高分辨率的alarm、sleep、gettimeofday、内置计时器
use Time::HiRes qw(usleep ualarm gettimeofday tv_interval nanosleep clock_gettime clock_getres clock_nanosleep clock stat);

use Time::HiRes后,此模块提供sleep(), alarm(), time()的增强版以取代perl内置的相应函数。其中sleep()和alarm()的参数可以是小数。比如sleep(0.1)表示休眠0.1秒,time()可以返回浮点数。

计算机中时间单位表述

秒的换算:ms(毫秒),μs(微秒),ns(纳秒),ps(皮秒)
皮秒,符号ps英语:picosecond .
1皮秒等于一万亿分之一秒10-12秒
1,000 皮秒 = 1纳秒
1,000,000 皮秒 = 1微秒
1,000,000,000 皮秒 = 1毫秒
1,000,000,000,000 皮秒 = 1秒

纳秒,符号ns英语:nanosecond .
1纳秒等于十亿分之一秒10-9秒
1 纳秒 = 1000皮秒
1,000 纳秒 = 1微秒
1,000,000 纳秒 = 1毫秒
1,000,000,000 纳秒 = 1秒

微秒,符号μs英语:microsecond .
1微秒等于一百万分之一秒10-6秒
0.000 001 微秒 = 1皮秒
0.001 微秒 = 1纳秒
1,000 微秒 = 1毫秒
1,000,000 微秒 = 1秒

毫秒,符号ms英语:millisecond .
1毫秒等于一千分之一秒10-3秒
0.000 000 001 毫秒 = 1皮秒
0.000 001 毫秒 = 1纳秒
0.001 毫秒 = 1微秒
1000 毫秒 = 1秒

参考来源:

Time::HiRes