Sign in to follow this  
Followers 0
IchBistTod

More accurate sleep

11 posts in this topic

I dunno if anyone else has posted this but here it is.

Very low cpu usage ~9MS off using sleep(not relative to amount of time).

func _Sleep($ms)
$timer = timerinit()
while timerdiff($timer) < $ms
sleep(1)
wend
endfunc

Max CPU usage, exact time, down to the nanosecond(not relative to amount of time).

func _Sleep($ms)
$timer = timerinit()
$ms = (($ms-0.1)+0.07)
while timerdiff($timer) < $ms
;nothing
wend
endfunc

tell me if you have any luck with it.

Green= My Sleep

Red=AU3 Sleep

Test(1000ms):

1000.00107962209

1001.00960277494

1000.00256635174

1000.00359289244

1000.0010265407

1000.00667251453

1000.00256635174

999.984088619205

1000.00110616279

1000.00872559592

1000.0010265407

999.297846163638

1000.0010265407

999.998973459304

1000.00153981104

999.993840755821

1000 <<<<<<<<<<<<<<<< PERFECT!

1000.01745119184

1000.00359289244

1000.00051327035

1000 <<<<<<<<<<<<<<<< PERFECT!

999.909664418714

1000.00153981104

1000.01539811045

1000.00205308139

999.993840755821

1000 <<<<<<<<<<<<<<<< PERFECT!

1000.01283175871

1000.00153981104

1000.00564597383

Test(30000ms):

30000.001539811

30011.3386552625

30000.0010265407

30003.2941690949

30000.0020530814

30008.3391033475

as you see my sleeps are never more than ~.003 off where as AU3 sleeps can be up to ~1.0 off on a simple 1 second sleep.

AU3 sleep shows much variation so when used for larger amounts of time,it can be very inaccurate.

AU3 sleep is offset relevant to the amount of time the sleep is called for, neither express this attribute.


[center][/center][center]=][u][/u][/center][center][/center]

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

FYI, AutoIt Sleep() has a minimum value of 15 ms and works in ~15 ms increments.

For $j = 5 To 50 Step 5
    For $i = 1 To 10
        $timer = TimerInit()
        Sleep($j)
        ConsoleWrite(TimerDiff($timer) & @CRLF)
    Next
Next
Edited by wraithdu

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

Moot. The point, that is. :(

Mute means "without voice" or silent.

As to the thread itself, monoceres has presented us with a special sleep that's accurate to 100 nanoseconds.

http://www.autoitscript.com/forum/index.php?showtopic=77905

Share this post


Link to post
Share on other sites

but it seems closer to 10 then 15. But thats on my side. Result might depend on the system.

Yeah, the 15ms was on my Win7 x64 desktop. Just tried on my Win7 x86 laptop, and it does seem closer to 10ms increments.

Sleep(0) is faster cause it's not sleeping at all (obviously).

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Yeah, the 15ms was on my Win7 x64 desktop. Just tried on my Win7 x86 laptop, and it does seem closer to 10ms increments.

Sleep(0) is faster cause it's not sleeping at all (obviously).

Not so obvious to anyone reading who doesn't know the API...

Just to add context to that for any readers (and yes I know this has been said thousands of times)

If you use the API, Sleep(0) will give up it's time-slice to a process of equal priority and puts the current thread in a Ready state [link], so it may or may not actually "sleep" with 0.

I don't know if AutoIt's Sleep() reflects the API function in that case or not.

Edit: typo, logic

Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Let me see If I get that in a correct perspective. If your doing a very cpu intensive task thats also hugging hogging up the system (kinda hard to do with AutoIt itself I think, Ergo: not tested), Using a sleep(0) might also (but not necessary) present a opportunity for other tasks to get some cpu time.

Making the system/Os/Other-Apps somewhat more responsive.?

And if there are no other apps/processes in need of some cpu time, Your process will run at just about the same speed.?

What I posted was mostly just taken from the MSDN page, so it should be roughly the "correct perspective" I hope.

1. Yes, for processes of the same priority.

2. Yes and No

About 2., the MSDN article says that besides giving up it's time it puts the thread into a ready state which might not immediately continue.

So, even if there were no other processes to give up the time to, it still may be delayed from continuing (possibly even after Sleep() ends).

The point I was trying to emphasize though, is that Sleep(0) is not a consistent delay; so it is the one condition that cannot be made "more accurate" in the case of this thread.

Correct me if I missed anything.

Edit: may->might

Edited by crashdemons

My Projects - WindowDarken (Darken except the active window) Yahsmosis Chat Client (Discontinued) StarShooter Game (Red alert! All hands to battlestations!) YMSG Protocol Support (Discontinued) Circular Keyboard and OSK example. (aka Iris KB) Target Screensaver Drive Toolbar Thingy Rollup Pro (Minimize-to-Titlebar & More!) 2D Launcher physics example Ascii Screenshot AutoIt3 Quine Example ("Is a Quine" is a Quine.) USB Lock (Another system keydrive - with a toast.)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

monoceres made a sleep down to 100 nano second sleep udf a while back http://www.autoitscript.com/forum/index.php?showtopic=77905

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0