FAQ 8.17 How can I measure time under a second?

FAQ 8.17 How can I measure time under a second?

am 05.11.2007 15:03:03 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.

Re: FAQ 8.17 How can I measure time under a second?

am 05.11.2007 21:57:37 von Ben Morrow

Quoth 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";

Since Time::HiRes can call gettimeofday(2) in a sane manner, wouldn't it
be better to remove the second part of this answer? Perhaps replace it
with a note

If you can't install Time::HiRes, and you are on a Unixish system,
you may be able to call gettimeofday(2) directly. See
L.

Ben

Re: FAQ 8.17 How can I measure time under a second?

am 07.11.2007 09:36:16 von brian d foy

In article <1nc405-qr4.ln1@osiris.mauzo.dyndns.org>, Ben Morrow
wrote:


> Since Time::HiRes can call gettimeofday(2) in a sane manner, wouldn't it
> be better to remove the second part of this answer? Perhaps replace it
> with a note
>
> If you can't install Time::HiRes, and you are on a Unixish system,
> you may be able to call gettimeofday(2) directly. See
> L.


Patched, thanks, :)