Qbasicnews.com

Full Version: SLEEP accuracy
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
I was fooling around in FBIde and decided to test iteration rates, accuracies, etc.
Turns out the SLEEP command is pretty inaccurate. If you plug into it the value 10000, it should delay 10000 milliseconds (10 seconds). Nope. It consistantly takes approximately 12.5 seconds.
Code:
CONST MILLISECONDS=10000
DIM AS DOUBLE t,t2
t=TIMER
SLEEP MILLISECONDS
t2=TIMER - t
PRINT t2
SLEEP
Unless there's something wrong with that code.
[EDIT]
Quote:The accuracy of SLEEP is variable depending on the OS cycle time (Windows NT/2K/XP: 15 ms, 9x/Me: 50 ms, Linux 10ms, DOS 55 ms).
Ya that has been brought up before on this site, if you need 100% accuracy then make your own or use some external library delay
Quote:I was fooling around in FBIde and decided to test iteration rates, accuracies, etc.
Turns out the SLEEP command is pretty inaccurate. If you plug into it the value 10000, it should delay 10000 milliseconds (10 seconds). Nope. It consistantly takes approximately 12.5 seconds.
Code:
CONST MILLISECONDS=10000
DIM AS DOUBLE t,t2
t=TIMER
SLEEP MILLISECONDS
t2=TIMER - t
PRINT t2
SLEEP
Unless there's something wrong with that code.
[EDIT]
The FreeBASIC Manual Wrote:The accuracy of SLEEP is variable depending on the OS cycle time (Windows NT/2K/XP: 15 ms, 9x/Me: 50 ms, Linux 10ms, DOS 55 ms).

t2 = TIMER - t and PRINT t2 take time, but 2.5 extra seconds is way too much.
Your code runs in 10.03 seconds for me, fairly accurate!
W2000 SP4, P4 1,4

Perhaps you're running it from an IDE and it's interfering?
Antoni: I tried it compiled and it gave me a worse result (~12.6 this time).
I've noticed that SLEEP is interrupted by user input.
Code:
do
    t= timer
    sleep 5000
    print timer-t
loop

Left alone, the program prints out about every 5 seconds, but pressing a key seems to 'wake' it up early. (pressing a key rapidly actually caused it to print out negative numbers, and that's confusing)

Is this a well known behavior?
Quote:I've noticed that SLEEP is interrupted by user input.
Code:
do
    t= timer
    sleep 5000
    print timer-t
loop

Left alone, the program prints out about every 5 seconds, but pressing a key seems to 'wake' it up early. (pressing a key rapidly actually caused it to print out negative numbers, and that's confusing)

Is this a well known behavior?
Yes, sleep is exited as soon as you press a key..

Add ,1 to the end:
sleep 5000, 1
to have it ignore keypresses

This is known, and desired behaviour.
Try this..

Joshy
Code:
CONST MILLISECONDS=10000
DIM AS DOUBLE t,t2
t=TIMER
SLEEP MILLISECONDS,1
t2=TIMER - t
PRINT t2
SLEEP
Amazing! That gives me a near-perfect result (~9.99 seconds)!
Okay, here's my theory: the SLEEP command spends time checking for keypresses, and so that slows it down. While disabled, it ignores that stuff and so is faster and more accurate?
Yes sir you get 100 points ;-)

Joshy
Pages: 1 2 3 4