Commit | Line | Data |
dcf686c9 |
1 | package Time::HiRes; |
2 | |
3 | use strict; |
105cd853 |
4 | use vars qw($VERSION $XS_VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD); |
dcf686c9 |
5 | |
6 | require Exporter; |
3f2ee006 |
7 | require DynaLoader; |
dcf686c9 |
8 | |
0cf8ddea |
9 | @ISA = qw(Exporter DynaLoader); |
dcf686c9 |
10 | |
11 | @EXPORT = qw( ); |
3c72ec00 |
12 | @EXPORT_OK = qw (usleep sleep ualarm alarm gettimeofday time tv_interval |
ced84e60 |
13 | getitimer setitimer nanosleep clock_gettime clock_getres |
170c5524 |
14 | clock clock_nanosleep |
ced84e60 |
15 | CLOCK_HIGHRES CLOCK_MONOTONIC CLOCK_PROCESS_CPUTIME_ID |
170c5524 |
16 | CLOCK_REALTIME CLOCK_SOFTTIME CLOCK_THREAD_CPUTIME_ID |
17 | CLOCK_TIMEOFDAY CLOCKS_PER_SEC |
3f2ee006 |
18 | ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF |
170c5524 |
19 | TIMER_ABSTIME |
3f2ee006 |
20 | d_usleep d_ualarm d_gettimeofday d_getitimer d_setitimer |
170c5524 |
21 | d_nanosleep d_clock_gettime d_clock_getres |
22 | d_clock d_clock_nanosleep); |
3f2ee006 |
23 | |
9b15102a |
24 | $VERSION = '1.87'; |
105cd853 |
25 | $XS_VERSION = $VERSION; |
26 | $VERSION = eval $VERSION; |
3c72ec00 |
27 | |
28 | sub AUTOLOAD { |
29 | my $constname; |
98b50af3 |
30 | ($constname = $AUTOLOAD) =~ s/.*:://; |
ced84e60 |
31 | # print "AUTOLOAD: constname = $constname ($AUTOLOAD)\n"; |
98b50af3 |
32 | die "&Time::HiRes::constant not defined" if $constname eq 'constant'; |
33 | my ($error, $val) = constant($constname); |
ced84e60 |
34 | # print "AUTOLOAD: error = $error, val = $val\n"; |
0cf8ddea |
35 | if ($error) { |
36 | my (undef,$file,$line) = caller; |
37 | die "$error at $file line $line.\n"; |
38 | } |
3c72ec00 |
39 | { |
40 | no strict 'refs'; |
41 | *$AUTOLOAD = sub { $val }; |
42 | } |
43 | goto &$AUTOLOAD; |
44 | } |
dcf686c9 |
45 | |
ced84e60 |
46 | sub import { |
47 | my $this = shift; |
48 | for my $i (@_) { |
170c5524 |
49 | if (($i eq 'clock_getres' && !&d_clock_getres) || |
50 | ($i eq 'clock_gettime' && !&d_clock_gettime) || |
51 | ($i eq 'clock_nanosleep' && !&d_clock_nanosleep) || |
52 | ($i eq 'clock' && !&d_clock) || |
53 | ($i eq 'nanosleep' && !&d_nanosleep) || |
54 | ($i eq 'usleep' && !&d_usleep) || |
55 | ($i eq 'ualarm' && !&d_ualarm)) { |
ced84e60 |
56 | require Carp; |
57 | Carp::croak("Time::HiRes::$i(): unimplemented in this platform"); |
58 | } |
59 | } |
60 | Time::HiRes->export_to_level(1, $this, @_); |
61 | } |
62 | |
0cf8ddea |
63 | bootstrap Time::HiRes; |
dcf686c9 |
64 | |
65 | # Preloaded methods go here. |
66 | |
67 | sub tv_interval { |
68 | # probably could have been done in C |
69 | my ($a, $b) = @_; |
70 | $b = [gettimeofday()] unless defined($b); |
71 | (${$b}[0] - ${$a}[0]) + ((${$b}[1] - ${$a}[1]) / 1_000_000); |
72 | } |
73 | |
dcf686c9 |
74 | # Autoload methods go after =cut, and are processed by the autosplit program. |
75 | |
76 | 1; |
77 | __END__ |
78 | |
79 | =head1 NAME |
80 | |
f7916ddb |
81 | Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers |
dcf686c9 |
82 | |
83 | =head1 SYNOPSIS |
84 | |
ced84e60 |
85 | use Time::HiRes qw( usleep ualarm gettimeofday tv_interval nanosleep |
170c5524 |
86 | clock_gettime clock_getres clock_nanosleep clock ); |
dcf686c9 |
87 | |
88 | usleep ($microseconds); |
44d3ce20 |
89 | nanosleep ($nanoseconds); |
dcf686c9 |
90 | |
91 | ualarm ($microseconds); |
92 | ualarm ($microseconds, $interval_microseconds); |
93 | |
94 | $t0 = [gettimeofday]; |
95 | ($seconds, $microseconds) = gettimeofday; |
96 | |
97 | $elapsed = tv_interval ( $t0, [$seconds, $microseconds]); |
98 | $elapsed = tv_interval ( $t0, [gettimeofday]); |
99 | $elapsed = tv_interval ( $t0 ); |
100 | |
101 | use Time::HiRes qw ( time alarm sleep ); |
3c72ec00 |
102 | |
dcf686c9 |
103 | $now_fractions = time; |
104 | sleep ($floating_seconds); |
105 | alarm ($floating_seconds); |
106 | alarm ($floating_seconds, $floating_interval); |
107 | |
3c72ec00 |
108 | use Time::HiRes qw( setitimer getitimer |
3f2ee006 |
109 | ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ITIMER_REALPROF ); |
3c72ec00 |
110 | |
111 | setitimer ($which, $floating_seconds, $floating_interval ); |
112 | getitimer ($which); |
113 | |
82cbdcc3 |
114 | $realtime = clock_gettime(CLOCK_REALTIME); |
115 | $resolution = clock_getres(CLOCK_REALTIME); |
ced84e60 |
116 | |
ff7df920 |
117 | clock_nanosleep(CLOCK_REALTIME, 1.5); |
118 | clock_nanosleep(CLOCK_REALTIME, time() + 10, TIMER_ABSTIME); |
170c5524 |
119 | |
120 | my $ticktock = clock(); |
121 | |
dcf686c9 |
122 | =head1 DESCRIPTION |
123 | |
4ed0e2d4 |
124 | The C<Time::HiRes> module implements a Perl interface to the |
44d3ce20 |
125 | C<usleep>, C<nanosleep>, C<ualarm>, C<gettimeofday>, and |
126 | C<setitimer>/C<getitimer> system calls, in other words, high |
127 | resolution time and timers. See the L</EXAMPLES> section below and the |
128 | test scripts for usage; see your system documentation for the |
129 | description of the underlying C<nanosleep> or C<usleep>, C<ualarm>, |
130 | C<gettimeofday>, and C<setitimer>/C<getitimer> calls. |
dcf686c9 |
131 | |
6937b144 |
132 | If your system lacks C<gettimeofday()> or an emulation of it you don't |
4ed0e2d4 |
133 | get C<gettimeofday()> or the one-argument form of C<tv_interval()>. |
82cbdcc3 |
134 | If your system lacks all of C<nanosleep()>, C<usleep()>, |
135 | C<select()>, and C<poll>, you don't get C<Time::HiRes::usleep()>, |
136 | C<Time::HiRes::nanosleep()>, or C<Time::HiRes::sleep()>. |
137 | If your system lacks both C<ualarm()> and C<setitimer()> you don't get |
44d3ce20 |
138 | C<Time::HiRes::ualarm()> or C<Time::HiRes::alarm()>. |
3f2ee006 |
139 | |
140 | If you try to import an unimplemented function in the C<use> statement |
141 | it will fail at compile time. |
142 | |
4ed0e2d4 |
143 | If your subsecond sleeping is implemented with C<nanosleep()> instead |
144 | of C<usleep()>, you can mix subsecond sleeping with signals since |
64a7a97c |
145 | C<nanosleep()> does not use signals. This, however, is not portable, |
146 | and you should first check for the truth value of |
4ed0e2d4 |
147 | C<&Time::HiRes::d_nanosleep> to see whether you have nanosleep, and |
148 | then carefully read your C<nanosleep()> C API documentation for any |
44d3ce20 |
149 | peculiarities. |
0be47ac6 |
150 | |
0cf8ddea |
151 | If you are using C<nanosleep> for something else than mixing sleeping |
152 | with signals, give some thought to whether Perl is the tool you should |
153 | be using for work requiring nanosecond accuracies. |
dcf686c9 |
154 | |
3c72ec00 |
155 | The following functions can be imported from this module. |
156 | No functions are exported by default. |
dcf686c9 |
157 | |
158 | =over 4 |
159 | |
160 | =item gettimeofday () |
161 | |
0be47ac6 |
162 | In array context returns a two-element array with the seconds and |
f7916ddb |
163 | microseconds since the epoch. In scalar context returns floating |
6937b144 |
164 | seconds like C<Time::HiRes::time()> (see below). |
dcf686c9 |
165 | |
166 | =item usleep ( $useconds ) |
167 | |
44d3ce20 |
168 | Sleeps for the number of microseconds (millionths of a second) |
169 | specified. Returns the number of microseconds actually slept. Can |
170c5524 |
170 | sleep for more than one second, unlike the C<usleep> system call. Can |
171 | also sleep for zero seconds, which often works like a I<thread yield>. |
172 | See also C<Time::HiRes::usleep()>, C<Time::HiRes::sleep()>, and |
173 | C<Time::HiRes::clock_nanosleep()>. |
44d3ce20 |
174 | |
175 | Do not expect usleep() to be exact down to one microsecond. |
176 | |
177 | =item nanosleep ( $nanoseconds ) |
178 | |
179 | Sleeps for the number of nanoseconds (1e9ths of a second) specified. |
180 | Returns the number of nanoseconds actually slept (accurate only to |
181 | microseconds, the nearest thousand of them). Can sleep for more than |
170c5524 |
182 | one second. Can also sleep for zero seconds, which often works like a |
183 | I<thread yield>. See also C<Time::HiRes::sleep()>, |
184 | C<Time::HiRes::usleep()>, and C<Time::HiRes::clock_nanosleep()>. |
44d3ce20 |
185 | |
186 | Do not expect nanosleep() to be exact down to one nanosecond. |
187 | Getting even accuracy of one thousand nanoseconds is good. |
dcf686c9 |
188 | |
189 | =item ualarm ( $useconds [, $interval_useconds ] ) |
190 | |
6937b144 |
191 | Issues a C<ualarm> call; the C<$interval_useconds> is optional and |
192 | will be zero if unspecified, resulting in C<alarm>-like behaviour. |
dcf686c9 |
193 | |
993164ab |
194 | Note that the interaction between alarms and sleeps is unspecified. |
64a7a97c |
195 | |
443572f5 |
196 | =item tv_interval |
197 | |
0be47ac6 |
198 | tv_interval ( $ref_to_gettimeofday [, $ref_to_later_gettimeofday] ) |
dcf686c9 |
199 | |
f7916ddb |
200 | Returns the floating seconds between the two times, which should have |
6937b144 |
201 | been returned by C<gettimeofday()>. If the second argument is omitted, |
f7916ddb |
202 | then the current time is used. |
dcf686c9 |
203 | |
204 | =item time () |
205 | |
f7916ddb |
206 | Returns a floating seconds since the epoch. This function can be |
6937b144 |
207 | imported, resulting in a nice drop-in replacement for the C<time> |
208 | provided with core Perl; see the L</EXAMPLES> below. |
dcf686c9 |
209 | |
6937b144 |
210 | B<NOTE 1>: This higher resolution timer can return values either less |
211 | or more than the core C<time()>, depending on whether your platform |
212 | rounds the higher resolution timer values up, down, or to the nearest second |
213 | to get the core C<time()>, but naturally the difference should be never |
ced84e60 |
214 | more than half a second. See also L</clock_getres>, if available |
215 | in your system. |
f7916ddb |
216 | |
6937b144 |
217 | B<NOTE 2>: Since Sunday, September 9th, 2001 at 01:46:40 AM GMT, when |
218 | the C<time()> seconds since epoch rolled over to 1_000_000_000, the |
0be47ac6 |
219 | default floating point format of Perl and the seconds since epoch have |
220 | conspired to produce an apparent bug: if you print the value of |
4ed0e2d4 |
221 | C<Time::HiRes::time()> you seem to be getting only five decimals, not |
222 | six as promised (microseconds). Not to worry, the microseconds are |
64a7a97c |
223 | there (assuming your platform supports such granularity in the first |
4ed0e2d4 |
224 | place). What is going on is that the default floating point format of |
225 | Perl only outputs 15 digits. In this case that means ten digits |
226 | before the decimal separator and five after. To see the microseconds |
227 | you can use either C<printf>/C<sprintf> with C<"%.6f">, or the |
228 | C<gettimeofday()> function in list context, which will give you the |
229 | seconds and microseconds as two separate values. |
389199d8 |
230 | |
dcf686c9 |
231 | =item sleep ( $floating_seconds ) |
232 | |
f7916ddb |
233 | Sleeps for the specified amount of seconds. Returns the number of |
64a7a97c |
234 | seconds actually slept (a floating point value). This function can |
235 | be imported, resulting in a nice drop-in replacement for the C<sleep> |
6937b144 |
236 | provided with perl, see the L</EXAMPLES> below. |
dcf686c9 |
237 | |
993164ab |
238 | Note that the interaction between alarms and sleeps is unspecified. |
64a7a97c |
239 | |
dcf686c9 |
240 | =item alarm ( $floating_seconds [, $interval_floating_seconds ] ) |
241 | |
6937b144 |
242 | The C<SIGALRM> signal is sent after the specified number of seconds. |
243 | Implemented using C<ualarm()>. The C<$interval_floating_seconds> argument |
244 | is optional and will be zero if unspecified, resulting in C<alarm()>-like |
dcf686c9 |
245 | behaviour. This function can be imported, resulting in a nice drop-in |
6937b144 |
246 | replacement for the C<alarm> provided with perl, see the L</EXAMPLES> below. |
dcf686c9 |
247 | |
64a7a97c |
248 | B<NOTE 1>: With some combinations of operating systems and Perl |
249 | releases C<SIGALRM> restarts C<select()>, instead of interrupting it. |
250 | This means that an C<alarm()> followed by a C<select()> may together |
251 | take the sum of the times specified for the the C<alarm()> and the |
252 | C<select()>, not just the time of the C<alarm()>. |
253 | |
993164ab |
254 | Note that the interaction between alarms and sleeps is unspecified. |
3f2ee006 |
255 | |
6937b144 |
256 | =item setitimer ( $which, $floating_seconds [, $interval_floating_seconds ] ) |
3c72ec00 |
257 | |
09fa32a4 |
258 | Start up an interval timer: after a certain time, a signal arrives, |
64a7a97c |
259 | and more signals may keep arriving at certain intervals. To disable |
260 | an "itimer", use C<$floating_seconds> of zero. If the |
261 | C<$interval_floating_seconds> is set to zero (or unspecified), the |
262 | timer is disabled B<after> the next delivered signal. |
3c72ec00 |
263 | |
6937b144 |
264 | Use of interval timers may interfere with C<alarm()>, C<sleep()>, |
265 | and C<usleep()>. In standard-speak the "interaction is unspecified", |
0be47ac6 |
266 | which means that I<anything> may happen: it may work, it may not. |
3c72ec00 |
267 | |
268 | In scalar context, the remaining time in the timer is returned. |
269 | |
270 | In list context, both the remaining time and the interval are returned. |
271 | |
4ed0e2d4 |
272 | There are usually three or four interval timers available: the |
273 | C<$which> can be C<ITIMER_REAL>, C<ITIMER_VIRTUAL>, C<ITIMER_PROF>, or |
274 | C<ITIMER_REALPROF>. Note that which ones are available depends: true |
275 | UNIX platforms usually have the first three, but (for example) Win32 |
276 | and Cygwin have only C<ITIMER_REAL>, and only Solaris seems to have |
277 | C<ITIMER_REALPROF> (which is used to profile multithreaded programs). |
3c72ec00 |
278 | |
993164ab |
279 | C<ITIMER_REAL> results in C<alarm()>-like behaviour. Time is counted in |
6937b144 |
280 | I<real time>; that is, wallclock time. C<SIGALRM> is delivered when |
3c72ec00 |
281 | the timer expires. |
282 | |
4ed0e2d4 |
283 | C<ITIMER_VIRTUAL> counts time in (process) I<virtual time>; that is, |
284 | only when the process is running. In multiprocessor/user/CPU systems |
285 | this may be more or less than real or wallclock time. (This time is |
286 | also known as the I<user time>.) C<SIGVTALRM> is delivered when the |
287 | timer expires. |
3c72ec00 |
288 | |
6937b144 |
289 | C<ITIMER_PROF> counts time when either the process virtual time or when |
0be47ac6 |
290 | the operating system is running on behalf of the process (such as I/O). |
291 | (This time is also known as the I<system time>.) (The sum of user |
6937b144 |
292 | time and system time is known as the I<CPU time>.) C<SIGPROF> is |
293 | delivered when the timer expires. C<SIGPROF> can interrupt system calls. |
3c72ec00 |
294 | |
295 | The semantics of interval timers for multithreaded programs are |
296 | system-specific, and some systems may support additional interval |
6937b144 |
297 | timers. See your C<setitimer()> documentation. |
3c72ec00 |
298 | |
299 | =item getitimer ( $which ) |
300 | |
6937b144 |
301 | Return the remaining time in the interval timer specified by C<$which>. |
3c72ec00 |
302 | |
303 | In scalar context, the remaining time is returned. |
304 | |
305 | In list context, both the remaining time and the interval are returned. |
6937b144 |
306 | The interval is always what you put in using C<setitimer()>. |
3c72ec00 |
307 | |
ced84e60 |
308 | =item clock_gettime ( $which ) |
309 | |
310 | Return as seconds the current value of the POSIX high resolution timer |
311 | specified by C<$which>. All implementations that support POSIX high |
312 | resolution timers are supposed to support at least the C<$which> value |
313 | of C<CLOCK_REALTIME>, which is supposed to return results close to the |
314 | results of C<gettimeofday>, or the number of seconds since 00:00:00:00 |
315 | January 1, 1970 Greenwich Mean Time (GMT). Do not assume that |
316 | CLOCK_REALTIME is zero, it might be one, or something else. |
317 | Another potentially useful (but not available everywhere) value is |
318 | C<CLOCK_MONOTONIC>, which guarantees a monotonically increasing time |
319 | value (unlike time(), which can be adjusted). See your system |
320 | documentation for other possibly supported values. |
321 | |
322 | =item clock_getres ( $which ) |
323 | |
324 | Return as seconds the resolution of the POSIX high resolution timer |
325 | specified by C<$which>. All implementations that support POSIX high |
326 | resolution timers are supposed to support at least the C<$which> value |
170c5524 |
327 | of C<CLOCK_REALTIME>, see L</clock_gettime>. |
328 | |
329 | =item clock_nanosleep ( $which, $seconds, $flags = 0) |
330 | |
331 | Sleeps for the number of seconds (1e9ths of a second) specified. |
332 | Returns the number of seconds actually slept. The $which is the |
333 | "clock id", as with clock_gettime() and clock_getres(). The flags |
334 | default to zero but C<TIMER_ABSTIME> can specified (must be exported |
335 | explicitly) which means that C<$nanoseconds> is not a time interval |
336 | (as is the default) but instead an absolute time. Can sleep for more |
337 | than one second. Can also sleep for zero seconds, which often works |
338 | like a I<thread yield>. See also C<Time::HiRes::sleep()>, |
339 | C<Time::HiRes::usleep()>, and C<Time::HiRes::nanosleep()>. |
340 | |
341 | Do not expect clock_nanosleep() to be exact down to one nanosecond. |
342 | Getting even accuracy of one thousand nanoseconds is good. |
343 | |
344 | =item clock() |
345 | |
346 | Return as seconds the I<process time> (user + system time) spent by |
347 | the process since the first call to clock() (the definition is B<not> |
348 | "since the start of the process", though if you are lucky these times |
349 | may be quite close to each other, depending on the system). What this |
350 | means is that you probably need to store the result of your first call |
351 | to clock(), and subtract that value from the following results of clock(). |
352 | |
353 | The time returned also includes the process times of the terminated |
354 | child processes for which wait() has been executed. This value is |
355 | somewhat like the second value returned by the times() of core Perl, |
356 | but not necessarily identical. Note that due to backward |
ff7df920 |
357 | compatibility limitations the returned value may wrap around at about |
358 | 2147 seconds or at about 36 minutes. |
ced84e60 |
359 | |
dcf686c9 |
360 | =back |
361 | |
362 | =head1 EXAMPLES |
363 | |
364 | use Time::HiRes qw(usleep ualarm gettimeofday tv_interval); |
365 | |
366 | $microseconds = 750_000; |
70cf0185 |
367 | usleep($microseconds); |
dcf686c9 |
368 | |
369 | # signal alarm in 2.5s & every .1s thereafter |
70cf0185 |
370 | ualarm(2_500_000, 100_000); |
dcf686c9 |
371 | |
372 | # get seconds and microseconds since the epoch |
70cf0185 |
373 | ($s, $usec) = gettimeofday(); |
dcf686c9 |
374 | |
375 | # measure elapsed time |
376 | # (could also do by subtracting 2 gettimeofday return values) |
377 | $t0 = [gettimeofday]; |
378 | # do bunch of stuff here |
379 | $t1 = [gettimeofday]; |
380 | # do more stuff here |
381 | $t0_t1 = tv_interval $t0, $t1; |
0be47ac6 |
382 | |
dcf686c9 |
383 | $elapsed = tv_interval ($t0, [gettimeofday]); |
384 | $elapsed = tv_interval ($t0); # equivalent code |
385 | |
386 | # |
387 | # replacements for time, alarm and sleep that know about |
388 | # floating seconds |
389 | # |
390 | use Time::HiRes; |
391 | $now_fractions = Time::HiRes::time; |
392 | Time::HiRes::sleep (2.5); |
393 | Time::HiRes::alarm (10.6666666); |
0be47ac6 |
394 | |
dcf686c9 |
395 | use Time::HiRes qw ( time alarm sleep ); |
396 | $now_fractions = time; |
397 | sleep (2.5); |
398 | alarm (10.6666666); |
399 | |
3c72ec00 |
400 | # Arm an interval timer to go off first at 10 seconds and |
401 | # after that every 2.5 seconds, in process virtual time |
402 | |
403 | use Time::HiRes qw ( setitimer ITIMER_VIRTUAL time ); |
404 | |
36d6c396 |
405 | $SIG{VTALRM} = sub { print time, "\n" }; |
3c72ec00 |
406 | setitimer(ITIMER_VIRTUAL, 10, 2.5); |
407 | |
1a7d3a53 |
408 | use Time::HiRes qw( clock_gettime clock_getres CLOCK_REALTIME ); |
409 | # Read the POSIX high resolution timer. |
410 | my $high = clock_getres(CLOCK_REALTIME); |
411 | # But how accurate we can be, really? |
412 | my $reso = clock_getres(CLOCK_REALTIME); |
ced84e60 |
413 | |
170c5524 |
414 | use Time::HiRes qw( clock_nanosleep TIMER_ABSTIME ); |
415 | clock_nanosleep(CLOCK_REALTIME, 1e6); |
416 | clock_nanosleep(CLOCK_REALTIME, 2e9, TIMER_ABSTIME); |
417 | |
418 | use Time::HiRes qw( clock ); |
419 | my $clock0 = clock(); |
420 | ... # Do something. |
421 | my $clock1 = clock(); |
422 | my $clockd = $clock1 - $clock0; |
423 | |
dcf686c9 |
424 | =head1 C API |
425 | |
426 | In addition to the perl API described above, a C API is available for |
427 | extension writers. The following C functions are available in the |
428 | modglobal hash: |
429 | |
430 | name C prototype |
431 | --------------- ---------------------- |
432 | Time::NVtime double (*)() |
06252d99 |
433 | Time::U2time void (*)(pTHX_ UV ret[2]) |
dcf686c9 |
434 | |
6937b144 |
435 | Both functions return equivalent information (like C<gettimeofday>) |
436 | but with different representations. The names C<NVtime> and C<U2time> |
dcf686c9 |
437 | were selected mainly because they are operating system independent. |
56c1b3bd |
438 | (C<gettimeofday> is Unix-centric, though some platforms like Win32 and |
439 | VMS have emulations for it.) |
dcf686c9 |
440 | |
6937b144 |
441 | Here is an example of using C<NVtime> from C: |
dcf686c9 |
442 | |
993164ab |
443 | double (*myNVtime)(); /* Returns -1 on failure. */ |
dcf686c9 |
444 | SV **svp = hv_fetch(PL_modglobal, "Time::NVtime", 12, 0); |
445 | if (!svp) croak("Time::HiRes is required"); |
446 | if (!SvIOK(*svp)) croak("Time::NVtime isn't a function pointer"); |
356234a5 |
447 | myNVtime = INT2PTR(double(*)(), SvIV(*svp)); |
dcf686c9 |
448 | printf("The current time is: %f\n", (*myNVtime)()); |
449 | |
db0b859f |
450 | =head1 DIAGNOSTICS |
451 | |
452 | =head2 negative time not invented yet |
453 | |
454 | You tried to use a negative time argument. |
455 | |
456 | =head2 internal error: useconds < 0 (unsigned ... signed ...) |
457 | |
458 | Something went horribly wrong-- the number of microseconds that cannot |
459 | become negative just became negative. Maybe your compiler is broken? |
460 | |
f03b998d |
461 | =head1 CAVEATS |
462 | |
6937b144 |
463 | Notice that the core C<time()> maybe rounding rather than truncating. |
d8cb5b61 |
464 | What this means is that the core C<time()> may be reporting the time |
465 | as one second later than C<gettimeofday()> and C<Time::HiRes::time()>. |
466 | |
467 | Adjusting the system clock (either manually or by services like ntp) |
468 | may cause problems, especially for long running programs that assume |
469 | a monotonously increasing time (note that all platforms do not adjust |
470 | time as gracefully as UNIX ntp does). For example in Win32 (and derived |
471 | platforms like Cygwin and MinGW) the Time::HiRes::time() may temporarily |
472 | drift off from the system clock (and the original time()) by up to 0.5 |
473 | seconds. Time::HiRes will notice this eventually and recalibrate. |
ced84e60 |
474 | Note that since Time::HiRes 1.77 the clock_gettime(CLOCK_MONOTONIC) |
1a7d3a53 |
475 | might help in this (in case your system supports CLOCK_MONOTONIC). |
f03b998d |
476 | |
26e22fd9 |
477 | =head1 SEE ALSO |
478 | |
ced84e60 |
479 | Perl modules L<BSD::Resource>, L<Time::TAI64>. |
480 | |
481 | Your system documentation for C<clock_gettime>, C<clock_settime>, |
482 | C<gettimeofday>, C<getitimer>, C<setitimer>, C<ualarm>. |
26e22fd9 |
483 | |
dcf686c9 |
484 | =head1 AUTHORS |
485 | |
486 | D. Wegscheid <wegscd@whirlpool.com> |
487 | R. Schertler <roderick@argon.org> |
488 | J. Hietaniemi <jhi@iki.fi> |
489 | G. Aas <gisle@aas.no> |
490 | |
3f2ee006 |
491 | =head1 COPYRIGHT AND LICENSE |
dcf686c9 |
492 | |
3f2ee006 |
493 | Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved. |
dcf686c9 |
494 | |
0cf8ddea |
495 | Copyright (c) 2002, 2003, 2004, 2005 Jarkko Hietaniemi. All rights reserved. |
dcf686c9 |
496 | |
3f2ee006 |
497 | This program is free software; you can redistribute it and/or modify |
498 | it under the same terms as Perl itself. |
dcf686c9 |
499 | |
500 | =cut |