lgodfrey Posted July 16, 2005 Author Share Posted July 16, 2005 API Names are case sensitive so: DllCall($dll, "none", "Sleep", "long", Sleep)<{POST_SNAPBACK}>Thanks Jon, that did it. I ran some tests, and as I suspected but not experienced enough to say, the kernel32 sleep method is essentially identical to AutoIT sleep function. This time, the actual sleep time is approximately multiples of 15 msec. The test results and code are below.So...back to the original reason for this post...anyone know of a way to get sleep(T), accurate to +/-1 msec, low cpu useage, for small T? that I could implement until(and if) AutoIT's sleep function gets upgraded?Here is the code and test results:expandcollapse popup;comparing kernel32 and AutoIT sleep functions for times less than 100 msec Dim $dll, $K, $Sleep, $SavedTimesFile = "C:\Times.csv", $TimesFileH, $KK, $JJ, $KLOOPS, $TimeData[1000], $Line = 0 $dll = DllOpen("kernel32.dll") For $KK = 1 To 81 Step 2 ;loop without sleep to check loop time overhead $KLOOPS = Int(1000 / $KK) ;total time for each average would be constant if sleep function was accurate $T0 = TimerInit() For $JJ = 1 To $KLOOPS Next $Line = $Line + 1 $TimeData[$Line] = "Loopin without sleep to show loop over head time is very small=, " & $KK & ", " & Round(TimerDiff($T0) / $KLOOPS, 3) & " , " & $KLOOPS & @CR ConsoleWrite($TimeData[$Line]) Next For $KK = 1 To 81 Step 2 ;sleep for various times using kernel32 sleep $T0 = TimerInit() $KLOOPS = Int(1000 / $KK) For $JJ = 1 To $KLOOPS DllCall($dll, "none", "Sleep", "long", $KK) Next $Line = $Line + 1 $TimeData[$Line] = "DLL Call for Sleep, sleep time parameter, average sleep time, number of values in average=, " & $KK & ", " & Round(TimerDiff($T0) / $KLOOPS, 1) & " , " & $KLOOPS & @CR ConsoleWrite($TimeData[$Line]) Next For $KK = 1 To 81 Step 2;sleep for various times using AutoIT sleep $T0 = TimerInit() $KLOOPS = Int(1000 / $KK) For $JJ = 1 To $KLOOPS Sleep($KK) Next $Line = $Line + 1 $TimeData[$Line] = "AutoIT Sleep, sleep time parameter, average sleep time, number of values in average=, " & $KK & ", " & Round(TimerDiff($T0) / $KLOOPS, 1) & " , " & $KLOOPS & @CR ConsoleWrite($TimeData[$Line]) Next $TimesFileH = FileOpen($SavedTimesFile, 2) For $KK = 1 To $line FileWriteLine($TimesFileH, $TimeData[$KK]) Next FileClose($TimesFileH) DllClose($dll) ExitHere is a table of the results. Column titles are:SLEEP TIME PARAMETER# OF LOOPS FOR AVERAGELOOP TIME WITHOUT SLEEPAVERAGE AUTOIT SLEEP TIMEAVERAGE DLL SLEEP TIME1 1000 0.01 15.6 15.63 333 0.005 15.7 15.65 200 0.006 15.6 15.67 142 0.006 15.6 15.69 111 0.006 15.7 15.611 90 0.006 15.6 15.613 76 0.006 15.6 15.615 66 0.006 15.6 15.617 58 0.007 31.2 31.219 52 0.007 31.2 31.321 47 0.008 31.3 31.223 43 0.065 31.3 31.225 40 0.007 31.3 31.327 37 0.007 31.3 31.229 34 0.016 31.2 31.331 32 0.007 31.2 31.233 30 0.008 46.8 46.935 28 0.007 46.8 46.937 27 0.008 46.9 46.939 25 0.008 47.1 46.941 24 0.007 46.8 46.943 23 0.008 46.9 47.245 22 0.007 46.8 46.547 21 0.008 62.4 49.949 20 0.008 62.5 62.451 19 0.008 62.5 62.553 18 0.009 62.5 62.555 18 0.011 62.5 62.557 17 0.008 62.7 62.559 16 0.008 62.5 62.561 16 0.009 62.6 62.563 15 0.009 77.9 7565 15 0.009 78.2 78.367 14 0.009 78 77.969 14 0.009 78.1 78.371 14 0.011 78.1 77.973 13 0.009 78.5 78.175 13 0.009 77.9 78.377 12 0.009 78.2 78.279 12 0.009 93.4 92.281 12 0.01 93.7 93.7 I'm, Lovin' IT, X Link to comment Share on other sites More sharing options...
lgodfrey Posted July 17, 2005 Author Share Posted July 17, 2005 I've got it!! Woops, I don't!!! Well, I thought I had it....Just use AutoITX3.Control as an object called from AutoIT, and I would have access to AutoITX3's Sleep function.It only worked for times less than 10 msec.The following code does indeed sleep for about 1 msec (albeit with Task Manager reporting near 100% cpu useage) just like it does when called from Excel. It does this for 1 through 9 msec parameter values.Dim $oAIX, $Dummy $oAIX = ObjCreate ("AutoItX3.Control") $Dummy = $oaix.Sleep (1) However, AutoITX3 sleep(10) call from Excel sleeps for 10.8 msec, called from within AutoIT, does "exactly" the same thing as AutoIT's sleep function (sleeps sort of multiples of 15 msec). This is strange indeed, but I checked everthing many times to make sure I was doing the tests right. I can send the code to anyone interested.SO, new request for advice: is there a way to access AutoITX3 sleep function from AutoIT and make it act the same as when called from Excel VBA?Best Regards I'm, Lovin' IT, X Link to comment Share on other sites More sharing options...
herewasplato Posted July 17, 2005 Share Posted July 17, 2005 ...(sleeps sort of multiples of 15 msec).<{POST_SNAPBACK}>from the "for what it is worth department" - more sleep numbers to look at:http://www.autoitscript.com/forum/index.ph...959entry90959later.... [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
lgodfrey Posted July 18, 2005 Author Share Posted July 18, 2005 from the "for what it is worth department" - more sleep numbers to look at:http://www.autoitscript.com/forum/index.ph...959entry90959later....<{POST_SNAPBACK}>Thanks for the link. Strange, the reported times there are multiples of 10 msec for small T, I always seem to get multiples of 15. I posted there about this.Regards, LarryPS comments there on finding the right search criteria are right...my original search did not find that thread, and it started 3 days prior to this one. I'm, Lovin' IT, X Link to comment Share on other sites More sharing options...
Administrators Jon Posted July 18, 2005 Administrators Share Posted July 18, 2005 I've got it!! Woops, I don't!!! Well, I thought I had it....Just use AutoITX3.Control as an object called from AutoIT, and I would have access to AutoITX3's Sleep function.It only worked for times less than 10 msec.The following code does indeed sleep for about 1 msec (albeit with Task Manager reporting near 100% cpu useage) just like it does when called from Excel. It does this for 1 through 9 msec parameter values.Dim $oAIX, $Dummy $oAIX = ObjCreate ("AutoItX3.Control") $Dummy = $oaix.Sleep (1) However, AutoITX3 sleep(10) call from Excel sleeps for 10.8 msec, called from within AutoIT, does "exactly" the same thing as AutoIT's sleep function (sleeps sort of multiples of 15 msec). This is strange indeed, but I checked everthing many times to make sure I was doing the tests right. I can send the code to anyone interested.SO, new request for advice: is there a way to access AutoITX3 sleep function from AutoIT and make it act the same as when called from Excel VBA?Best RegardsThe AutoItX version of Sleep uses the same "Sleep" code as that of SendKeyDelay, WinWaitDelay etc which works like this:if timetosleep < 15ms then Sleep(timetosleep) else start = getCPUtimevalue current = start while (current-start) < timetosleep WINAPI Sleep(0) current - getCPUtimervalue wend end ifSleep(0) is special - it just gives up control of the thread if some other app is waiting otherwise carry on - but the above loop will give the impression of 100% cpu usage. This is not the case however, i've run a game with no loss in fps while testing this Deployment Blog: https://www.autoitconsulting.com/site/blog/ SCCM SDK Programming: https://www.autoitconsulting.com/site/sccm-sdk/ Link to comment Share on other sites More sharing options...
lgodfrey Posted July 19, 2005 Author Share Posted July 19, 2005 The AutoItX version of Sleep uses the same "Sleep" code as that of SendKeyDelay, WinWaitDelay etc which works like this:if timetosleep < 15ms then Sleep(timetosleep) else start = getCPUtimevalue current = start while (current-start) < timetosleep WINAPI Sleep(0) current - getCPUtimervalue wend end ifSleep(0) is special - it just gives up control of the thread if some other app is waiting otherwise carry on - but the above loop will give the impression of 100% cpu usage. This is not the case however, i've run a game with no loss in fps while testing this <{POST_SNAPBACK}> Thanks, Jon. Ya, I thought it was something like this, but is the inequality in the first line reversed? Also, is the WINAPI sleep the same as a kernel32 sleep? (my testing of kernel32 sleep gives "identical" timing as AutoIT sleep) My testing suggests that AutoITX form is more like: if timetosleep > 10 ms then Sleep(timetosleep) else start = getCPUtimevalue current = start while (current-start) < timetosleep WINAPI Sleep(0) current = getCPUtimervalue wend end ifBesides the change to 10msec, > to <, when I call it from Excel2003 VBA it seems to give quite accurate/reproducible sleep times (about equal to timetosleep+.6 (+/-.1) for timetosleep>9. This suggests to me that the X version does not use exactly the same algorithm as the script version for sleeps>9. Is it possible for AutoIT to use kernel32 sleep and AutoITX use WINAPI (if indeed these are different).Your posting also confirms an earlier request for info that SendKeyDelay uses such a loop for small delays (I was hoping to do an ugly work around by sending dummy key strokes to get short sleep times, but the cpu useage showed up)AutoIT doesn't have the if/else, right, it just has the sleep(timetosleep)?Also re cpu useage, ya I understand that it is not really 100%, it is just as much cpu time as is available. Doesn't this mean that the actual "sleep" time for short sleeps will change depending on how long it takes to complete any other thread, right? This may not be an issue if HT is turned on, but I have HT turned off since if I don't, my Excel will max out at about 60% cpu useage, and it needs to go to about 80% to run my vba without any slow down due to parallel processes. I use VBA "DO EVENTS" to let my parallel scripts get cpu time, "but only when I can afford to let them". With HT turned off, I would imagine apps tend to "fight" over cpu time than when on. By the way, HT off or on makes no difference to my timing test results.RegardsLarry I'm, Lovin' IT, X Link to comment Share on other sites More sharing options...
lgodfrey Posted July 21, 2005 Author Share Posted July 21, 2005 (edited) I think I came up with a function that gives good accuracy, low cpu time, all sleep times.I posted it in scripts and scraps as "Here's an alternet to Sleep"Regards (edit, here's a link: Here) Larry Edited July 21, 2005 by lgodfrey I'm, Lovin' IT, X Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now