FAQ
Author: comdog
Date: Wed Nov 7 00:35:12 2007
New Revision: 10183

Modified:
perlfaq/trunk/perlfaq8.pod

Log:
* perlfaq8: How can I measure time under a second?
+ Ben Morrow suggested cutting out the long gettimeofday example
since Time::HiRes already does that and it's a core module.



Modified: perlfaq/trunk/perlfaq8.pod
==============================================================================
--- perlfaq/trunk/perlfaq8.pod (original)
+++ perlfaq/trunk/perlfaq8.pod Wed Nov 7 00:35:12 2007
@@ -461,48 +461,25 @@
system "trn comp.lang.perl.misc";

=head2 How can I sleep() or alarm() for under a second?
+X<Time::HiRes> X<BSD::Itimer> X<sleep> X<select>

-If you want finer granularity than the 1 second that the sleep()
-function provides, the easiest way is to use the select() function as
-documented in L<perlfunc/"select">. Try the Time::HiRes and
-the BSD::Itimer modules (available from CPAN, and starting from
-Perl 5.8 Time::HiRes is part of the standard distribution).
+If you want finer granularity than the 1 second that the C<sleep()>
+function provides, the easiest way is to use the C<select()> function as
+documented in L<perlfunc/"select">. Try the C<Time::HiRes> and
+the C<BSD::Itimer> modules (available from CPAN, and starting from
+Perl 5.8 C<Time::HiRes> is part of the standard distribution).

=head2 How can I measure time under a second?
+X<Time::HiRes> X<BSD::Itimer> X<sleep> X<select>

-In general, you may not be able to. The Time::HiRes module (available
-from CPAN, and starting from Perl 5.8 part of the standard distribution)
-provides this functionality for some systems.
-
-If your system supports both the syscall() function in Perl as well as
-a system call like gettimeofday(2), then you may be able to do
-something like this:
-
- require 'sys/syscall.ph';
-
- $TIMEVAL_T = "LL";
-
- $done = $start = pack($TIMEVAL_T, ());
-
- syscall(&SYS_gettimeofday, $start, 0) != -1
- or die "gettimeofday: $!";
-
- ##########################
- # DO YOUR OPERATION HERE #
- ##########################
-
- syscall( &SYS_gettimeofday, $done, 0) != -1
- or die "gettimeofday: $!";
-
- @start = unpack($TIMEVAL_T, $start);
- @done = unpack($TIMEVAL_T, $done);
-
- # fix microseconds
- for ($done[1], $start[1]) { $_ /= 1_000_000 }
+(contributed by brian d foy)

- $delta_time = sprintf "%.4f", ($done[0] + $done[1] )
- -
- ($start[0] + $start[1] );
+The C<Time::HiRes> module (part of the standard distribution as of
+Perl 5.8) measures time with the C<gettimeofday()> system call, which
+returns the time in microseconds since the epoch. If you can't install
+C<Time::HiRes> for older Perls and you are on a Unixish system, you
+may be able to call C<gettimeofday(2)> directly. See
+L<perlfunc/syscall>.

=head2 How can I do an atexit() or setjmp()/longjmp()? (Exception handling)

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcvs-perlfaq @
categoriesperl
postedNov 7, '07 at 8:35a
activeNov 7, '07 at 8:35a
posts1
users1
websitelearn.perl.org...

1 user in discussion

Comdog: 1 post

People

Translate

site design / logo © 2018 Grokbase