Greetings,

I am in need of accurate time measurement down to 0.1ms - Generally Time::HiRes does seem to do pretty well, but (using the below code) sometimes gets spikes of 15.625ms.

In reading the module notes, it says Do not expect usleep() to be exact down to one microsecond which I'm perfectly fine with, that makes sense. but these spikes, 15.625ms (most commonly), are way above 50-100 microseconds... which was the kind of precision I was expecting/hoping for.

Questions:
Is my code actually measuring the accuracy of Time::HiRes or are these spikes due to something else?
If the code is fine, how might I be able to better determine what the current time is with accuracy down to 0.1ms? (To clarify, I don't specifically need to sleep for an exact amount of time, I need a means of calculating what the current time is down to 0.1ms accuracy at certain events.)

Incidentally, on my last test, only 63 samples out of 20,000 were unexpected, which is ok, but I'd like to do better than that if possible...

Thanks in advance

Update:
I'm using ActivePerl 5.8.8 and 5.10.1
use strict; use warnings; use Time::HiRes ; my $secs_old = 1; my $usecs_old = 0.000001; my $sleep_amt = 1; for (1..100_000){ my ($secs_new, $usecs_new) = Time::HiRes::gettimeofday(); my $secs_diff = $secs_new - $secs_old - $sleep_amt; my $usecs_diff = $usecs_new - $usecs_old; $usecs_diff = $usecs_diff / 1000; print "[$secs_diff] [$usecs_diff]\n"; $secs_old = $secs_new; $usecs_old = $usecs_new; #Time::HiRes::usleep(1_000_000); #I found usleep tends to have +even less accurate results than the builtin sleep function... sleep $sleep_amt; } __END__ [0] [0] [0] [15.625] [0] [0] .... [0] [0] [0] [15.625] [0] [0] [0] [0] [0] [0] [0] [0] [0] [265.625] [0] [0] [0] [0] ... etc

In reply to Unexpected Timing spikes using Time::HiRes by desemondo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.