in reply to Re^2: Waiting for Alarm
in thread Waiting for Alarm
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:
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.my $deadline = time() + 10; while( 1 ) { sleep 1 while time() < $deadline; $deadline = time() +10; ## Do the something }
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Waiting for Alarm
by bobf (Monsignor) on Feb 17, 2008 at 03:52 UTC | |
by BrowserUk (Patriarch) on Feb 17, 2008 at 04:25 UTC |