📄 hires.pm
字号:
C<select()>, not just the time of the C<alarm()>.Note that the interaction between alarms and sleeps is unspecified.=item setitimer ( $which, $floating_seconds [, $interval_floating_seconds ] )Start up an interval timer: after a certain time, a signal arrives,and more signals may keep arriving at certain intervals. To disablean "itimer", use C<$floating_seconds> of zero. If theC<$interval_floating_seconds> is set to zero (or unspecified), thetimer is disabled B<after> the next delivered signal.Use of interval timers may interfere with C<alarm()>, C<sleep()>,and C<usleep()>. In standard-speak the "interaction is unspecified",which means that I<anything> may happen: it may work, it may not.In scalar context, the remaining time in the timer is returned.In list context, both the remaining time and the interval are returned.There are usually three or four interval timers available: theC<$which> can be C<ITIMER_REAL>, C<ITIMER_VIRTUAL>, C<ITIMER_PROF>, orC<ITIMER_REALPROF>. Note that which ones are available depends: trueUNIX platforms usually have the first three, but (for example) Win32and Cygwin have only C<ITIMER_REAL>, and only Solaris seems to haveC<ITIMER_REALPROF> (which is used to profile multithreaded programs).C<ITIMER_REAL> results in C<alarm()>-like behaviour. Time is counted inI<real time>; that is, wallclock time. C<SIGALRM> is delivered whenthe timer expires.C<ITIMER_VIRTUAL> counts time in (process) I<virtual time>; that is,only when the process is running. In multiprocessor/user/CPU systemsthis may be more or less than real or wallclock time. (This time isalso known as the I<user time>.) C<SIGVTALRM> is delivered when thetimer expires.C<ITIMER_PROF> counts time when either the process virtual time or whenthe operating system is running on behalf of the process (such as I/O).(This time is also known as the I<system time>.) (The sum of usertime and system time is known as the I<CPU time>.) C<SIGPROF> isdelivered when the timer expires. C<SIGPROF> can interrupt system calls.The semantics of interval timers for multithreaded programs aresystem-specific, and some systems may support additional intervaltimers. See your C<setitimer()> documentation.=item getitimer ( $which )Return the remaining time in the interval timer specified by C<$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 C<setitimer()>.=item clock_gettime ( $which )Return as seconds the current value of the POSIX high resolution timerspecified by C<$which>. All implementations that support POSIX highresolution timers are supposed to support at least the C<$which> valueof C<CLOCK_REALTIME>, which is supposed to return results close to theresults of C<gettimeofday>, or the number of seconds since 00:00:00:00January 1, 1970 Greenwich Mean Time (GMT). Do not assume thatCLOCK_REALTIME is zero, it might be one, or something else.Another potentially useful (but not available everywhere) value isC<CLOCK_MONOTONIC>, which guarantees a monotonically increasing timevalue (unlike time(), which can be adjusted). See your systemdocumentation for other possibly supported values.=item clock_getres ( $which )Return as seconds the resolution of the POSIX high resolution timerspecified by C<$which>. All implementations that support POSIX highresolution timers are supposed to support at least the C<$which> valueof C<CLOCK_REALTIME>, see L</clock_gettime>.=item clock_nanosleep ( $which, $seconds, $flags = 0)Sleeps for the number of seconds (1e9ths of a second) specified.Returns the number of seconds actually slept. The $which is the"clock id", as with clock_gettime() and clock_getres(). The flagsdefault to zero but C<TIMER_ABSTIME> can specified (must be exportedexplicitly) which means that C<$nanoseconds> is not a time interval(as is the default) but instead an absolute time. Can sleep for morethan one second. Can also sleep for zero seconds, which often workslike a I<thread yield>. See also C<Time::HiRes::sleep()>,C<Time::HiRes::usleep()>, and C<Time::HiRes::nanosleep()>.Do not expect clock_nanosleep() to be exact down to one nanosecond.Getting even accuracy of one thousand nanoseconds is good.=item clock()Return as seconds the I<process time> (user + system time) spent bythe process since the first call to clock() (the definition is B<not>"since the start of the process", though if you are lucky these timesmay be quite close to each other, depending on the system). What thismeans is that you probably need to store the result of your first callto clock(), and subtract that value from the following results of clock().The time returned also includes the process times of the terminatedchild processes for which wait() has been executed. This value issomewhat like the second value returned by the times() of core Perl,but not necessarily identical. Note that due to backwardcompatibility limitations the returned value may wrap around at about2147 seconds or at about 36 minutes.=back=head1 EXAMPLES use Time::HiRes qw(usleep ualarm gettimeofday tv_interval); $microseconds = 750_000; usleep($microseconds); # signal alarm in 2.5s & every .1s thereafter ualarm(2_500_000, 100_000); # get seconds and microseconds since the epoch ($s, $usec) = gettimeofday(); # measure elapsed time # (could also do by subtracting 2 gettimeofday return values) $t0 = [gettimeofday]; # do bunch of stuff here $t1 = [gettimeofday]; # do more stuff here $t0_t1 = tv_interval $t0, $t1; $elapsed = tv_interval ($t0, [gettimeofday]); $elapsed = tv_interval ($t0); # equivalent code # # replacements for time, alarm and sleep that know about # floating seconds # use Time::HiRes; $now_fractions = Time::HiRes::time; Time::HiRes::sleep (2.5); Time::HiRes::alarm (10.6666666); use Time::HiRes qw ( time alarm sleep ); $now_fractions = time; sleep (2.5); alarm (10.6666666); # Arm an interval timer to go off first at 10 seconds and # after that every 2.5 seconds, in process virtual time use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time ); $SIG{VTALRM} = sub { print time, "\n" }; setitimer(ITIMER_VIRTUAL, 10, 2.5); use Time::HiRes qw( clock_gettime clock_getres CLOCK_REALTIME ); # Read the POSIX high resolution timer. my $high = clock_getres(CLOCK_REALTIME); # But how accurate we can be, really? my $reso = clock_getres(CLOCK_REALTIME); use Time::HiRes qw( clock_nanosleep TIMER_ABSTIME ); clock_nanosleep(CLOCK_REALTIME, 1e6); clock_nanosleep(CLOCK_REALTIME, 2e9, TIMER_ABSTIME); use Time::HiRes qw( clock ); my $clock0 = clock(); ... # Do something. my $clock1 = clock(); my $clockd = $clock1 - $clock0;=head1 C APIIn addition to the perl API described above, a C API is available forextension writers. The following C functions are available in themodglobal hash: name C prototype --------------- ---------------------- Time::NVtime double (*)() Time::U2time void (*)(pTHX_ UV ret[2])Both functions return equivalent information (like C<gettimeofday>)but with different representations. The names C<NVtime> and C<U2time>were selected mainly because they are operating system independent.(C<gettimeofday> is Unix-centric, though some platforms like Win32 andVMS have emulations for it.)Here is an example of using C<NVtime> from C: double (*myNVtime)(); /* Returns -1 on failure. */ SV **svp = hv_fetch(PL_modglobal, "Time::NVtime", 12, 0); if (!svp) croak("Time::HiRes is required"); if (!SvIOK(*svp)) croak("Time::NVtime isn't a function pointer"); myNVtime = INT2PTR(double(*)(), SvIV(*svp)); printf("The current time is: %f\n", (*myNVtime)());=head1 DIAGNOSTICS=head2 negative time not invented yetYou tried to use a negative time argument.=head2 internal error: useconds < 0 (unsigned ... signed ...)Something went horribly wrong-- the number of microseconds that cannotbecome negative just became negative. Maybe your compiler is broken?=head1 CAVEATSNotice that the core C<time()> maybe rounding rather than truncating.What this means is that the core C<time()> may be reporting the timeas one second later than C<gettimeofday()> and C<Time::HiRes::time()>.Adjusting the system clock (either manually or by services like ntp)may cause problems, especially for long running programs that assumea monotonously increasing time (note that all platforms do not adjusttime as gracefully as UNIX ntp does). For example in Win32 (and derivedplatforms like Cygwin and MinGW) the Time::HiRes::time() may temporarilydrift off from the system clock (and the original time()) by up to 0.5seconds. Time::HiRes will notice this eventually and recalibrate.Note that since Time::HiRes 1.77 the clock_gettime(CLOCK_MONOTONIC)might help in this (in case your system supports CLOCK_MONOTONIC).=head1 SEE ALSOPerl modules L<BSD::Resource>, L<Time::TAI64>.Your system documentation for C<clock_gettime>, C<clock_settime>,C<gettimeofday>, C<getitimer>, C<setitimer>, C<ualarm>.=head1 AUTHORSD. Wegscheid <wegscd@whirlpool.com>R. Schertler <roderick@argon.org>J. Hietaniemi <jhi@iki.fi>G. Aas <gisle@aas.no>=head1 COPYRIGHT AND LICENSECopyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.Copyright (c) 2002, 2003, 2004, 2005 Jarkko Hietaniemi. All rights reserved.This program is free software; you can redistribute it and/or modifyit under the same terms as Perl itself.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -