We have monitoring scripts that need to be run every second. The scripts execution takes longer than a second, but that is not a problem.

So we have need a function that would run the script every second. The interval must be as precise as possible. I know that the preciseness depends on CPU, RAM, CMOS etc. I don’t care about these.

Currently, we use a script, let’s call it negative_timer. It works quite precisely, but the Time::HiRes::sleep from time to time complains that negative time not invented yet. A second using this script is in average about 0.000000676723442 less than a real second.

The problem is that the negative_timer substracts a second from a $max value. Sometimes the NTP kicks in and it makes the trouble. We don’t case if a second would be longer/shorter when the NTP updates the time.

Actually, we don’t need any $max value, as we run the monitoring script indefinitely (i.e. until seldom reboot or maintaince).

Could anyone help me out in this?

Thank you in advance.

The negative_timer script:

use strict; use warnings; use Time::HiRes qw/time sleep/; sub negative_timer { my $max = $_[0]; my @time_test; my $start = time(); for (my $i = 0; $i < $max; $i++) { $start += 1; sleep $start - time(); $time_test[$i] = time(); } return @time_test; }

In reply to The most precise second (timer) by tukusejssirs

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.