4 use vars qw($VERSION $XS_VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD);
9 @ISA = qw(Exporter DynaLoader);
12 @EXPORT_OK = qw (usleep sleep ualarm alarm gettimeofday time tv_interval
14 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF
15 d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer
19 $XS_VERSION = $VERSION;
20 $VERSION = eval $VERSION;
24 ($constname = $AUTOLOAD) =~ s/.*:://;
25 die "&Time::HiRes::constant not defined" if $constname eq 'constant';
26 my ($error, $val) = constant($constname);
27 if ($error) { die $error; }
30 *$AUTOLOAD = sub { $val };
35 bootstrap Time::HiRes;
37 # Preloaded methods go here.
40 # probably could have been done in C
42 $b = [gettimeofday()] unless defined($b);
43 (${$b}[0] - ${$a}[0]) + ((${$b}[1] - ${$a}[1]) / 1_000_000);
46 # Autoload methods go after =cut, and are processed by the autosplit program.
53 Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers
57 use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
59 usleep ($microseconds);
61 ualarm ($microseconds);
62 ualarm ($microseconds, $interval_microseconds);
65 ($seconds, $microseconds) = gettimeofday;
67 $elapsed = tv_interval ( $t0, [$seconds, $microseconds]);
68 $elapsed = tv_interval ( $t0, [gettimeofday]);
69 $elapsed = tv_interval ( $t0 );
71 use Time::HiRes qw ( time alarm sleep );
73 $now_fractions = time;
74 sleep ($floating_seconds);
75 alarm ($floating_seconds);
76 alarm ($floating_seconds, $floating_interval);
78 use Time::HiRes qw( setitimer getitimer
79 ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF );
81 setitimer ($which, $floating_seconds, $floating_interval );
86 The C<Time::HiRes> module implements a Perl interface to the usleep,
87 ualarm, gettimeofday, and setitimer/getitimer system calls. See the
88 EXAMPLES section below and the test scripts for usage; see your system
89 documentation for the description of the underlying nanosleep or usleep,
90 ualarm, gettimeofday, and setitimer/getitimer calls.
92 If your system lacks gettimeofday(2) or an emulation of it you don't
93 get gettimeofday() or the one-arg form of tv_interval(). If you don't
94 have nanosleep() or usleep(3) or select(2) you don't get Time::HiRes::usleep()
95 or sleep(). If your system don't have ualarm(3) or setitimer(2) you
96 don't get Time::HiRes::ualarm() or alarm().
98 If you try to import an unimplemented function in the C<use> statement
99 it will fail at compile time.
101 If your subsecond sleeping is implemented with nanosleep() instead of
102 usleep(), you can mix subsecond sleeping with signals since
103 nanosleep() does not use signals. This, however, is unportable
104 behavior, and you should first check for the truth value of
105 C<&Time::HiRes::d_nanosleep> to see whether you have nanosleep,
106 and then read carefully your nanosleep() C API documentation for
107 any peculiarities. (There is no separate interface to call nanosleep();
108 just use Time::HiRes::sleep() or usleep() with small enough values. Also,
109 think twice whether using nanosecond accuracies in a Perl program is what
110 you should be doing.)
112 The following functions can be imported from this module.
113 No functions are exported by default.
117 =item gettimeofday ()
119 In array context returns a 2 element array with the seconds and
120 microseconds since the epoch. In scalar context returns floating
121 seconds like Time::HiRes::time() (see below).
123 =item usleep ( $useconds )
125 Sleeps for the number of microseconds specified. Returns the number
126 of microseconds actually slept. Can sleep for more than one second
127 unlike the usleep system call. See also Time::HiRes::sleep() below.
129 =item ualarm ( $useconds [, $interval_useconds ] )
131 Issues a ualarm call; interval_useconds is optional and will be 0 if
132 unspecified, resulting in alarm-like behaviour.
136 C<tv_interval ( $ref_to_gettimeofday [, $ref_to_later_gettimeofday] )>
138 Returns the floating seconds between the two times, which should have
139 been returned by gettimeofday(). If the second argument is omitted,
140 then the current time is used.
144 Returns a floating seconds since the epoch. This function can be
145 imported, resulting in a nice drop-in replacement for the C<time>
146 provided with core Perl, see the EXAMPLES below.
148 B<NOTE 1>: this higher resolution timer can return values either less or
149 more than the core time(), depending on whether your platforms rounds
150 the higher resolution timer values up, down, or to the nearest to get
151 the core time(), but naturally the difference should be never more than
154 B<NOTE 2>: Since Sunday, September 9th, 2001 at 01:46:40 AM GMT
155 (when the time() seconds since epoch rolled over to 1_000_000_000),
156 the default floating point format of Perl and the seconds since epoch
157 have conspired to produce an apparent bug: if you print the value of
158 Time::HiRes::time() you seem to be getting only five decimals, not six
159 as promised (microseconds). Not to worry, the microseconds are there
160 (assuming your platform supports such granularity). What is going on
161 is that the default floating point format of Perl only outputs 15
162 digits. In this case that means ten digits before the decimal
163 separator and five after. To see the microseconds you can use either
164 printf/sprintf with C<%.6f>, or the gettimeofday() function in list
165 context, which will give you the seconds and microseconds as two
168 =item sleep ( $floating_seconds )
170 Sleeps for the specified amount of seconds. Returns the number of
171 seconds actually slept (a floating point value). This function can be
172 imported, resulting in a nice drop-in replacement for the C<sleep>
173 provided with perl, see the EXAMPLES below.
175 =item alarm ( $floating_seconds [, $interval_floating_seconds ] )
177 The SIGALRM signal is sent after the specified number of seconds.
178 Implemented using ualarm(). The $interval_floating_seconds argument
179 is optional and will be 0 if unspecified, resulting in alarm()-like
180 behaviour. This function can be imported, resulting in a nice drop-in
181 replacement for the C<alarm> provided with perl, see the EXAMPLES below.
183 B<NOTE 1>: With some platform - Perl release combinations select()
184 gets restarted by SIGALRM, instead of dropping out of select().
185 This means that an alarm() followed by a select() may together take
186 the sum of the times specified for the the alarm() and the select(),
187 not just the time of the alarm().
191 C<setitimer ( $which, $floating_seconds [, $interval_floating_seconds ] )>
193 Start up an interval timer: after a certain time, a signal arrives,
194 and more signals may keep arriving at certain intervals. To disable
195 a timer, use time of zero. If interval is set to zero (or unspecified),
196 the timer is disabled B<after> the next delivered signal.
198 Use of interval timers may interfere with alarm(), sleep(), and usleep().
199 In standard-speak the "interaction is unspecified", which means that
200 I<anything> may happen: it may work, it may not.
202 In scalar context, the remaining time in the timer is returned.
204 In list context, both the remaining time and the interval are returned.
206 There are usually three or four interval timers available: the $which
207 can be ITIMER_REAL, ITIMER_VIRTUAL, ITIMER_PROF, or ITIMER_REALPROF.
208 Note that which ones are available depends: true UNIX platforms have
209 usually all first three, but for example Win32 and Cygwin only have
210 ITIMER_REAL, and only Solaris seems to have ITIMER_REALPROF (which is
211 used to profile multithreaded programs).
213 ITIMER_REAL results in alarm()-like behavior. Time is counted in
214 I<real time>, that is, wallclock time. SIGALRM is delivered when
217 ITIMER_VIRTUAL counts time in (process) I<virtual time>, that is, only
218 when the process is running. In multiprocessor/user/CPU systems this
219 may be more or less than real or wallclock time. (This time is also
220 known as the I<user time>.) SIGVTALRM is delivered when the timer expires.
222 ITIMER_PROF counts time when either the process virtual time or when
223 the operating system is running on behalf of the process (such as
224 I/O). (This time is also known as the I<system time>.) (Collectively
225 these times are also known as the I<CPU time>.) SIGPROF is delivered
226 when the timer expires. SIGPROF can interrupt system calls.
228 The semantics of interval timers for multithreaded programs are
229 system-specific, and some systems may support additional interval
230 timers. See your setitimer() documentation.
232 =item getitimer ( $which )
234 Return the remaining time in the interval timer specified by $which.
236 In scalar context, the remaining time is returned.
238 In list context, both the remaining time and the interval are returned.
239 The interval is always what you put in using setitimer().
245 use Time::HiRes qw(usleep ualarm gettimeofday tv_interval);
247 $microseconds = 750_000;
248 usleep $microseconds;
250 # signal alarm in 2.5s & every .1s thereafter
251 ualarm 2_500_000, 100_000;
253 # get seconds and microseconds since the epoch
254 ($s, $usec) = gettimeofday;
256 # measure elapsed time
257 # (could also do by subtracting 2 gettimeofday return values)
258 $t0 = [gettimeofday];
259 # do bunch of stuff here
260 $t1 = [gettimeofday];
262 $t0_t1 = tv_interval $t0, $t1;
264 $elapsed = tv_interval ($t0, [gettimeofday]);
265 $elapsed = tv_interval ($t0); # equivalent code
268 # replacements for time, alarm and sleep that know about
272 $now_fractions = Time::HiRes::time;
273 Time::HiRes::sleep (2.5);
274 Time::HiRes::alarm (10.6666666);
276 use Time::HiRes qw ( time alarm sleep );
277 $now_fractions = time;
281 # Arm an interval timer to go off first at 10 seconds and
282 # after that every 2.5 seconds, in process virtual time
284 use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time );
286 $SIG{VTLARM} = sub { print time, "\n" };
287 setitimer(ITIMER_VIRTUAL, 10, 2.5);
291 In addition to the perl API described above, a C API is available for
292 extension writers. The following C functions are available in the
296 --------------- ----------------------
297 Time::NVtime double (*)()
298 Time::U2time void (*)(UV ret[2])
300 Both functions return equivalent information (like C<gettimeofday>)
301 but with different representations. The names C<NVtime> and C<U2time>
302 were selected mainly because they are operating system independent.
303 (C<gettimeofday> is Un*x-centric.)
305 Here is an example of using NVtime from C:
307 double (*myNVtime)();
308 SV **svp = hv_fetch(PL_modglobal, "Time::NVtime", 12, 0);
309 if (!svp) croak("Time::HiRes is required");
310 if (!SvIOK(*svp)) croak("Time::NVtime isn't a function pointer");
311 myNVtime = INT2PTR(double(*)(), SvIV(*svp));
312 printf("The current time is: %f\n", (*myNVtime)());
316 Notice that the core time() maybe rounding rather than truncating.
317 What this means that the core time() may be giving time one second
318 later than gettimeofday(), also known as Time::HiRes::time().
322 D. Wegscheid <wegscd@whirlpool.com>
323 R. Schertler <roderick@argon.org>
324 J. Hietaniemi <jhi@iki.fi>
325 G. Aas <gisle@aas.no>
327 =head1 COPYRIGHT AND LICENSE
329 Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.
331 Copyright (c) 2002 Jarkko Hietaniemi. All rights reserved.
333 This program is free software; you can redistribute it and/or modify
334 it under the same terms as Perl itself.