Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The interval must be as precise as possible.

I think that this is causing some confusion in this thread, and we should take a moment to define the problem better. See e.g. Accuracy and precision - as several people have commented, if you want precise measurement of time, then really the only way to go about it is a real-time OS and/or other custom circuitry connected to an atomic clock, high-quality GPS receiver, etc. However:

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. ... I know that the preciseness depends on CPU, RAM, CMOS etc. I don’t care about these.

This, along with the code loop you showed, leads me to believe that precision isn't the central point of your question, it seems to me more like you're worried about a systematic and cumulative error, which would mean you're actually more worried about trueness.

In other words: You want something like for (1..100) { sleep( 1 + $correction_factor ) } to take as close to 100 wall clock seconds as possible. And at the same time, perhaps it's not so important to you if each delay has e.g. ±0.1 seconds precision, as long as they even out, in other words, it's ok if 10 of the delays take 1.1 seconds, as long as 10 other delays take 0.9 seconds.

Is this interpretation of your problem correct? In that case, IMHO sleep is not the right tool for the job, as I showed you'll need something that works with the computer's wall clock time.

And keeping the computer's wall clock time in sync with real-world time is a whole different can of worms! But in general that's what NTP is for, and if you're having trouble with that, as you seem to indicate, then perhaps it's best to look at that as a separate topic. (Update: Actually, on a second reading, apparently it's the corrections made to the system clock by NTP that seem to be giving you the trouble? That seems strange, if you're really running chronyd and no other NTP client on this machine. But in any case, everything above still stands.)

Update: I'm putting this in my top level reply because IMHO it is the best answer to your question: I only realized further down in this thread that clock_nanosleep from Time::HiRes is probably what you're looking for.


In reply to Re: The most precise second (timer) by haukex
in thread 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 06:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found