package Time::HiRes; use strict; use vars qw($VERSION $XS_VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD); require Exporter; use XSLoader; @ISA = qw(Exporter); @EXPORT = qw( ); @EXPORT_OK = qw (usleep sleep ualarm alarm gettimeofday time tv_interval getitimer setitimer ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF); $VERSION = '1.20_00'; $XS_VERSION = $VERSION; $VERSION = eval $VERSION; sub AUTOLOAD { my $constname; ($constname= $AUTOLOAD) =~ s/.*:://; my $val = constant($constname, @_ ? $_[0] : 0); if ($!) { my ($pack,$file,$line) = caller; die "Your vendor has not defined Time::HiRes macro $constname, used at $file line $line.\n"; } { no strict 'refs'; *$AUTOLOAD = sub { $val }; } goto &$AUTOLOAD; } XSLoader::load 'Time::HiRes', $XS_VERSION; # Preloaded methods go here. sub tv_interval { # probably could have been done in C my ($a, $b) = @_; $b = [gettimeofday()] unless defined($b); (${$b}[0] - ${$a}[0]) + ((${$b}[1] - ${$a}[1]) / 1_000_000); } # Autoload methods go after =cut, and are processed by the autosplit program. 1; __END__ =head1 NAME Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers =head1 SYNOPSIS use Time::HiRes qw( usleep ualarm gettimeofday tv_interval ); usleep ($microseconds); ualarm ($microseconds); ualarm ($microseconds, $interval_microseconds); $t0 = [gettimeofday]; ($seconds, $microseconds) = gettimeofday; $elapsed = tv_interval ( $t0, [$seconds, $microseconds]); $elapsed = tv_interval ( $t0, [gettimeofday]); $elapsed = tv_interval ( $t0 ); use Time::HiRes qw ( time alarm sleep ); $now_fractions = time; sleep ($floating_seconds); alarm ($floating_seconds); alarm ($floating_seconds, $floating_interval); use Time::HiRes qw( setitimer getitimer ITIMER_REAL ITIMER_VIRTUAL ITIMER_PROF ); setitimer ($which, $floating_seconds, $floating_interval ); getitimer ($which); =head1 DESCRIPTION The C module implements a Perl interface to the usleep, ualarm, gettimeofday, and setitimer/getitimer system calls. See the EXAMPLES section below and the test scripts for usage; see your system documentation for the description of the underlying usleep, ualarm, gettimeofday, and setitimer/getitimer calls. If your system lacks gettimeofday(2) or an emulation of it you don't get gettimeofday() or the one-arg form of tv_interval(). If you don't have usleep(3) or select(2) you don't get usleep() or sleep(). If your system don't have ualarm(3) or setitimer(2) you don't get ualarm() or alarm(). If you try to import an unimplemented function in the C statement it will fail at compile time. The following functions can be imported from this module. No functions are exported by default. =over 4 =item gettimeofday () In array context returns a 2 element array with the seconds and microseconds since the epoch. In scalar context returns floating seconds like Time::HiRes::time() (see below). =item usleep ( $useconds ) Sleeps for the number of microseconds specified. Returns the number of microseconds actually slept. Can sleep for more than one second unlike the usleep system call. See also Time::HiRes::sleep() below. =item ualarm ( $useconds [, $interval_useconds ] ) Issues a ualarm call; interval_useconds is optional and will be 0 if unspecified, resulting in alarm-like behaviour. =item tv_interval C 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. =item time () Returns a floating seconds since the epoch. This function can be imported, resulting in a nice drop-in replacement for the C