Know-How für Ihr Projekt

Perl Documentation


Time::HiRes - High resolution alarm, sleep, gettimeofday, interval timers


use Time::HiRes qw( usleep ualarm gettimeofday tv_interval nanosleep
		      clock_gettime clock_getres clock_nanosleep clock
                    stat lstat );
usleep ($microseconds);
nanosleep ($nanoseconds);
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 );
setitimer ($which, $floating_seconds, $floating_interval );
getitimer ($which);
use Time::HiRes qw( clock_gettime clock_getres clock_nanosleep
                    ITIMER_REALPROF );
$realtime   = clock_gettime(CLOCK_REALTIME);
$resolution = clock_getres(CLOCK_REALTIME);
clock_nanosleep(CLOCK_REALTIME, 1.5e9);
clock_nanosleep(CLOCK_REALTIME, time()*1e9 + 10e9, TIMER_ABSTIME);
my $ticktock = clock();
use Time::HiRes qw( stat lstat );
my @stat = stat("file");
my @stat = stat(FH);
my @stat = lstat("file");


The Time::HiRes module implements a Perl interface to the usleep, nanosleep, ualarm, gettimeofday, and setitimer/getitimer system calls, in other words, high resolution time and timers. See the "EXAMPLES" section below and the test scripts for usage; see your system documentation for the description of the underlying nanosleep or usleep, ualarm, gettimeofday, and setitimer/getitimer calls.

If your system lacks gettimeofday() or an emulation of it you don't get gettimeofday() or the one-argument form of tv_interval(). If your system lacks all of nanosleep(), usleep(), select(), and poll, you don't get Time::HiRes::usleep(), Time::HiRes::nanosleep(), or Time::HiRes::sleep(). If your system lacks both ualarm() and setitimer() you don't get Time::HiRes::ualarm() or Time::HiRes::alarm().

If you try to import an unimplemented function in the use statement it will fail at compile time.

If your subsecond sleeping is implemented with nanosleep() instead of usleep(), you can mix subsecond sleeping with signals since nanosleep() does not use signals. This, however, is not portable, and you should first check for the truth value of &Time::HiRes::d_nanosleep to see whether you have nanosleep, and then carefully read your nanosleep() C API documentation for any peculiarities.

If you are using nanosleep for something else than mixing sleeping with signals, give some thought to whether Perl is the tool you should be using for work requiring nanosecond accuracies.

Remember that unless you are working on a hard realtime system, any clocks and timers will be imprecise, especially so if you are working in a pre-emptive multiuser system. Understand the difference between wallclock time and process time (in UNIX-like systems the sum of user and system times). Any attempt to sleep for X seconds will most probably end up sleeping more than that, but don't be surprised if you end up sleeping slightly less.

The following functions can be imported from this module. No functions are exported by default.


$microseconds = 750_000;
# signal alarm in 2.5s & every .1s thereafter
ualarm(2_500_000, 100_000);
# cancel that ualarm
# 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);	# 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_gettime(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;
use Time::HiRes qw( stat );
my ($atime, $mtime, $ctime) = (stat("istics"))[8, 9, 10];


In addition to the perl API described above, a C API is available for extension writers. The following C functions are available in the modglobal hash:

name             C prototype
Time::NVtime     NV (*)()
Time::U2time     void (*)(pTHX_ UV ret[2])

Both functions return equivalent information (like gettimeofday) but with different representations. The names NVtime and U2time were selected mainly because they are operating system independent. (gettimeofday is Unix-centric, though some platforms like Win32 and VMS have emulations for it.)

Here is an example of using NVtime from C:

NV (*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(NV(*)(), SvIV(*svp));
printf("The current time is: %" NVff "\n", (*myNVtime)());


useconds or interval more than ...

In ualarm() you tried to use number of microseconds or interval (also in microseconds) more than 1_000_000 and setitimer() is not available in your system to emulate that case.

negative time not invented yet

You tried to use a negative time argument.

internal error: useconds < 0 (unsigned ... signed ...)

Something went horribly wrong-- the number of microseconds that cannot become negative just became negative. Maybe your compiler is broken?

useconds or uinterval equal to or more than 1000000

In some platforms it is not possible to get an alarm with subsecond resolution and later than one second.

unimplemented in this platform

Some calls simply aren't available, real or emulated, on every platform.


Notice that the core time() maybe rounding rather than truncating. What this means is that the core time() may be reporting the time as one second later than gettimeofday() and Time::HiRes::time().

Adjusting the system clock (either manually or by services like ntp) may cause problems, especially for long running programs that assume a monotonously increasing time (note that all platforms do not adjust time as gracefully as UNIX ntp does). For example in Win32 (and derived platforms like Cygwin and MinGW) the Time::HiRes::time() may temporarily drift off from the system clock (and the original time()) by up to 0.5 seconds. 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).

Some systems have APIs but not implementations: for example QNX and Haiku have the interval timer APIs but not the functionality.

In OS X clock_getres(), clock_gettime() and clock_nanosleep() are emulated using the Mach timers; as a side effect of being emulated the CLOCK_REALTIME and CLOCK_MONOTONIC are the same timer.


Perl modules BSD::Resource, Time::TAI64.

Your system documentation for clock, clock_gettime, clock_getres, clock_nanosleep, clock_settime, getitimer, gettimeofday, setitimer, sleep, stat, ualarm.


D. Wegscheid <> R. Schertler <> J. Hietaniemi <> G. Aas <>


Copyright (c) 1996-2002 Douglas E. Wegscheid. All rights reserved.

Copyright (c) 2002, 2003, 2004, 2005, 2006, 2007, 2008 Jarkko Hietaniemi. All rights reserved.

