FAQ 8.17 How can I measure time under a second?

FAQ 8.17 How can I measure time under a second?

am 21.01.2008 21:03:02 von PerlFAQ Server

This is an excerpt from the latest version perlfaq8.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

------------------------------------------------------------ --------

8.17: How can I measure time under a second?

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 }

$delta_time = sprintf "%.4f", ($done[0] + $done[1] )
-
($start[0] + $start[1] );



------------------------------------------------------------ --------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.