Time::AutoRes - use Time::HiRes or fall back to core code
use Time::AutoRes qw(sleep time alarm); sleep(1.5); $now = time; alarm($now + 2.5);
Time::AutoRes provides access to most of the functions that may be imported from Time::HiRes (see list below). If Time::HiRes isn't available, Time::AutoRes silently falls back to core Perl functions; when this happens, it tries to emulate Time::HiRes by rounding non-integers up or down in such a way as to approximate the non-integer value on average over repeated calls.
For example, if you call
usleep(3.4), and if Time::HiRes is not available, there's a 40% chance of sleeping for 4 seconds, and a 60% chance of sleeping for only 3 seconds. If you call
usleep(3.4) repeatedly, the average delay will tend toward 3.4 seconds.
Sleep the given number of sleeps. If the interval is not an integer, and Time::HiRes is not available, Time::AutoRes will randomize the delay as described above so that repeated calls with the same interval can be expected to sleep the specified interval on average.
Seleep the given number of microseconds. There are one million microseconds in a second. Randomness is used when Time::HiRes is not available and a non-integer argument is given, in exactly the same way as for Time::AutoRes::sleep.
Arranges to have a SIGALRM delivered to this process after the specified number of seconds have elapsed. Randomness is used when appropriate, as for Time::AutoRes::sleep.
alarmbut in microseconds rather than seconds. Again, randomness is used when appropriate.
Returns the number of non-leap seconds since the epoch.
This simply calls Time::HiRes::time if it's available, or CORE::Time if not.
Note: This is the only exported function that never uses randomness!
gettimeofday(), tv_interval(), getitimer() and setitimer() aren't implemented.
Paul Hofman (nkuitse AT cpan DOT org).
Copyright 2004 Paul M. Hoffman.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.