Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Measuring time intervals using Time::HiRes -- time vs. gettimeofday and tv_interval

by hrr (Monk)
on Jul 18, 2006 at 18:59 UTC ( [id://562099]=perlquestion: print w/replies, xml ) Need Help??

hrr has asked for the wisdom of the Perl Monks concerning the following question:

In Time::Hires, there are two simple ways to measure time intervals, namely
use Time::HiRes qw(gettimeofday tv_interval); $t0 = [gettimeofday]; $elapsed = tv_interval($t0)
or
use Time::HiRes qw(time); $t0 = time; $elapsed = time - $t0;
Is one of these two methods preferrable, considering accuracy and performance? As both gettimeofday and time provide accuracy up to microseconds, it seems to me that the accuracy of both time intervals are equivalent.

Thank you for your help!

Update: added use Time::HiRes to the code above.

Replies are listed 'Best First'.
Re: Measuring time intervals using Time::HiRes -- time vs. gettimeofday and tv_interval
by shonorio (Hermit) on Jul 18, 2006 at 19:14 UTC
    By Benchmark, the second one is faster than the first code. If think you could benchmark you code with both code to be sure.

    use Time::HiRes qw(time); use Benchmark qw(:all) ; # Use Perl code in strings... cmpthese(-10, { 'Hires' => sub { my $t0 = [Time::HiRes::gettimeofday]; my $elapsed = Time::HiRes::tv_interval($t0)} , 'Time' => sub { my $t0 = time; my $elapsed = time - $t0; }, });
    ... result ...
    Rate Hires Time Hires 100260/s -- -58% Time 240043/s 139% --
    Solli Moreira Honorio
    Sao Paulo - Brazil
      Thank you for this hint! I obtained similar benchmark results as you did.

      Surprsingly, using Time::HiRes::time instead of the standard time, the performance is much better (but still worse than tv_interval):
      Rate Hires Time Hires 98201/s -- -55% Time 220149/s 124% --
        Vice versa. Look at the Rate column — it shows the speed of the function! The more times a second the function is called, the faster it is :) So in this table (produced by the benchmark involving all three variants)
        Rate interval HiRes::time buitin time() interval 88470/s -- -53% -95% HiRes::time 188363/s 113% -- -89% buitin time() 1753092/s 1882% 831% --
        builtin time() is 9 times faster than Time::HiRes::time, which is nearly 2 times faster than the weird variant with tv_interval!

        Benchmark code is here:


             s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print
Re: Measuring time intervals using Time::HiRes -- time vs. gettimeofday and tv_interval
by kwaping (Priest) on Jul 18, 2006 at 22:53 UTC
    Here's the source for tv_interval:
    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); }
    That should explain everything, I think.

    ---
    It's all fine and dandy until someone has to look at the code.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://562099]
Approved by Hue-Bond
Front-paged by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-03-29 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found