in reply to Re: Waiting for Alarm
in thread Waiting for Alarm

Using sleep (or usleep from Time::HiRes) is how I've done it in the past, but the problem is accumulating time inaccuracy do to delays in processing.

For a bit more context, I'm taking measurements from electronic test equipment using the GPIB bus. I want to take those readings, say, every 10 seconds. The time it takes to tell the instrument to send a reading, for the instrument to process that request, and then return the result, is somewhat variable. So it's very hard to calibrate the sleep time to keep accuracy.

What I want to do is trigger the measurement cycle every X seconds regardless of how long (less than X, obviously) the measurement process takes.

In one previous iteration, I used HiRes gettime calls before and after the read/write cycle to determine how long the processing took, and used that to adjust the sleep time, but that seemed an awfully awkward way to go. I was hoping that a recurring alarm (which the HiRes setItimer function allows) would be a more elegant way to go.

Thanks! Hope this helps clarify.

Replies are listed 'Best First'.
Re^3: Waiting for Alarm
by BrowserUk (Patriarch) on Feb 16, 2008 at 19:24 UTC
    What I want to do is trigger the measurement cycle every X seconds regardless of how long (less than X, obviously) the measurement process takes.

    That's why I used a while loop sleeping for 1 second each time to accumulate the 10 seconds, rather than a 10 second sleep:

    my $deadline = time() + 10; while( 1 ) { sleep 1 while time() < $deadline; $deadline = time() +10; ## Do the something }
    This way, no matter how long "Do something" takes, so long as it's less that 10 seconds, the next reading will be initiated on time. To within one second.

    Eg. If it takes 3 seconds to process, the while loop will iterate 7 times. If it takes 7 seconds, the loop will iterate 3 times only.

    If you need to get more accurate, then use Time::HiRes and sleep for 1/10 of a second in the while loop. It will be 10 times more accurate and still consume almost imeasurable cpu.

    Need more accurate still? Then sleep for 1/100th of a second. It will still consume very little cpu and be another order of magnitude more accurate.

    Beyond that, you start getting into the realms of how long it takes to read the clock, affecting your accuracy, but if you need accuracy beyond 1/1000th of a second, you should probably be using C or assembler.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Need more accurate still? Then sleep for 1/100th of a second. It will still consume very little cpu and be another order of magnitude more accurate.

      One approach to achieve high accuracy while reducing the amount of calls to sleep and time is to sleep for long times initially and decrease them as you get closer to the time to "fire".

      For example, if you want to call a routine every 10 seconds at a resolution of 1/100th of a second, then you could first sleep for 8 seconds, then for 0.2 seconds until within 0.2 sec, then for 0.01 seconds. (These are arbitrary values, of course.)

      This could be premature optimization, though. :-)

        Something along the lines of:

        print $target = time + 10; print $s and sleep $s while ( $s = ($target - time )/2 ) > 0; print time;; 1203221999.59375 5 2.49999403953552 1.24999451637268 0.624994039535522 0.312494516372681 0.15625 0.0781184434890747 0.0390595197677612 0.0155709981918335 0.00779902935028076 1203221999.59376

        would work quite well. If the "do something" might take longer than half the delay time, then use a larger divisor:

        print $target = time + 10; print $s and sleep $s while ( $s = ($target - time )/3 ) > 0; print time;; 1203222233.07813 3.33329796791077 2.21874562899272 1.47916666666667 0.984371026357015 0.656246026357015 0.437496026357015 0.29166833559672 0.192704677581787 0.125013987223307 0.0833380222320557 0.0520946979522705 0.0311883290608724 0.0208296775817871 0.010399341583252 0.0052033265431722 1.98682149251302e-006 1203222233.07822 print $target = time + 10; print $s and sleep $s while ( $s = ($target - time )/4 ) > 0; print time;; 1203222257.71875 2.4999732375145 1.87499701976776 1.40624725818634 1.05468475818634 0.78905975818634 0.58984100818634 0.44140350818634 0.328112483024597 0.246071517467499 0.183622241020203 0.136751472949982 0.10155975818634 0.0742244720458984 0.0546977519989014 0.0386837720870972 0.0273575186729431 0.0195417404174805 0.011723518371582 0.00780248641967773 0.00391250848770142 1.59740447998047e-005 1203222257.71881

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^3: Waiting for Alarm
by pc88mxer (Vicar) on Feb 17, 2008 at 17:29 UTC
    Look into the setitimer function in the POSIX module. It will give you a stream of signals at repeated intervals. This might be a real slick solution combined with the pause suggestion made by traveler.