monoceres Posted August 8, 2008 Share Posted August 8, 2008 (edited) My god I was surprised when I found this function, it's frickin' awesome. It allows you to sleep down to 100 nanoseconds! UDF: ; #FUNCTION#;=============================================================================== ; ; Name...........: _HighPrecisionSleep() ; Description ...: Sleeps down to 0.1 microseconds ; Syntax.........: _HighPrecisionSleep( $iMicroSeconds, $hDll=False) ; Parameters ....: $iMicroSeconds - Amount of microseconds to sleep ; $hDll - Can be supplied so the UDF doesn't have to re-open the dll all the time. ; Return values .: None ; Author ........: Andreas Karlsson (monoceres) ; Modified.......: ; Remarks .......: Even though this has high precision you need to take into consideration that it will take some time for autoit to call the function. ; Related .......: ; Link ..........; ; Example .......; No ; ;;========================================================================================== Func _HighPrecisionSleep($iMicroSeconds,$hDll=False) Local $hStruct, $bLoaded If Not $hDll Then $hDll=DllOpen("ntdll.dll") $bLoaded=True EndIf $hStruct=DllStructCreate("int64 time;") DllStructSetData($hStruct,"time",-1*($iMicroSeconds*10)) DllCall($hDll,"dword","ZwDelayExecution","int",0,"ptr",DllStructGetPtr($hStruct)) If $bLoaded Then DllClose($hDll) EndFunc No msdn link for this one, since there is none Note: Even though it supports down to 100 ns sleep don't count on that for being correct in practise because autoit will take some time to call the function (and all the other sutff that are in the UDF). For example on my machine, it took autoit 260 microseconds to call the function. Edited October 13, 2008 by monoceres genius257 1 Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
martin Posted August 8, 2008 Share Posted August 8, 2008 My god I was surprised when I found this function, it's frickin' awesome. It allows you to sleep down to 100 nanoseconds! UDF: ; #FUNCTION#;=============================================================================== ; ; Name...........: _HighPrecisionSleep() ; Description ...: Sleeps down to 0.1 microseconds ; Syntax.........: _HighPrecisionSleep( $iMicroSeconds, $hDll=False) ; Parameters ....: $iMicroSeconds - Amount of microseconds to sleep ; $hDll - Can be supplied so the UDF doesn't have to re-open the dll all the time. ; Return values .: None ; Author ........: Andreas Karlsson (monoceres) ; Modified.......: ; Remarks .......: Even though this has high precision you need to take into consideration that it will take some time for autoit to call the function. ; Related .......: ; Link ..........; ; Example .......; No ; ;;========================================================================================== Func _HighPrecisionSleep($iMicroSeconds,$hDll=False) Local $hStruct, $bLoaded If Not $hDll Then $hDll=DllOpen("ntdll.dll") $bLoaded=True EndIf $hStruct=DllStructCreate("int64 time;") DllStructSetData($hStruct,"time",-1*($iMicroSeconds*10)) DllCall($hDll,"dword","ZwDelayExecution","int",0,"ptr",DllStructGetPtr($hStruct)) If $bLoaded Then DllClose($hDll) EndFunc No msdn link for this one, since there are none Note: Even though it supports down to 100 ns sleep don't count on that for being correct in practise because autoit will take some time to call the function (and all the other sutff that are in the UDF). For example on my machine, it took autoit 260 microseconds to call the function.Very interesting. I'm having a bit of a problem finding a use for this. Do you understand how to use the ZwDelayExecution function with positive values? (I don't.) Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
monoceres Posted August 8, 2008 Author Share Posted August 8, 2008 Very interesting. I'm having a bit of a problem finding a use for this. Do you understand how to use the ZwDelayExecution function with positive values? (I don't.)It's not completely useless. Now you can create high precision timers without eating up or the cpu or create very tight loops without getting the CPU up to 100%.I have no idea how to use positive values. According to this thread (also the thread where I found out about the function) on CodeGuru, positive values are "the absolute time to wake up". I have no idea how to calculate this (maybe nanoseconds since postix or something). Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
Szhlopp Posted August 8, 2008 Share Posted August 8, 2008 It's not completely useless. Now you can create high precision timers without eating up or the cpu or create very tight loops without getting the CPU up to 100%.I have no idea how to use positive values. According to this thread (also the thread where I found out about the function) on CodeGuru, positive values are "the absolute time to wake up". I have no idea how to calculate this (maybe nanoseconds since postix or something).Crazy... Nanoseconds = ?Microseconds = ?I'm a newb... RegEx/RegExRep Tester!Nerd Olympics - Community App!Login UDFMemory UDF - "Game.exe+753EC" - CE pointer to AU3Password Manager W/ SourceDataFiler - Include files in your au3!--- Was I helpful? Click the little green '+' Link to comment Share on other sites More sharing options...
monoceres Posted August 8, 2008 Author Share Posted August 8, 2008 Crazy... Nanoseconds = ?Microseconds = ?I'm a newb...Oh, you need to study the SI prefixes mLipok 1 Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
JRowe Posted August 9, 2008 Share Posted August 9, 2008 Microsecond = 1/1,000,000th of a second (one millionth) Nanosecond = 1/1,000,000,000th of a second (one billionth) The ms usually referred to in computer languages is millisecond, or one thousandth of a second. Nanosecond precision is , well, wow. [center]However, like ninjas, cyber warriors operate in silence.AutoIt Chat Engine (+Chatbot) , Link Grammar for AutoIt , Simple Speech RecognitionArtificial Neural Networks UDF , Bayesian Networks UDF , Pattern Matching UDFTransparent PNG GUI Elements , Au3Irrlicht 2Advanced Mouse Events MonitorGrammar Database GeneratorTransitions & Tweening UDFPoker Hand Evaluator[/center] Link to comment Share on other sites More sharing options...
Szhlopp Posted August 9, 2008 Share Posted August 9, 2008 (edited) Oh, you need to study the SI prefixes I have a zeptosecond script Lol... Sorry, I should have thought about just looking those up again.... Since I'm a idiot. How did you measure the time it took for that to process? Edited August 9, 2008 by Szhlopp RegEx/RegExRep Tester!Nerd Olympics - Community App!Login UDFMemory UDF - "Game.exe+753EC" - CE pointer to AU3Password Manager W/ SourceDataFiler - Include files in your au3!--- Was I helpful? Click the little green '+' Link to comment Share on other sites More sharing options...
BrettF Posted August 9, 2008 Share Posted August 9, 2008 (edited) Just a question, why would you want to pause for a 100 nanoseconds? I can't see a real use for that much precision... But otherwise this is awesome! Edited August 9, 2008 by BrettF Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
monoceres Posted August 9, 2008 Author Share Posted August 9, 2008 (edited) I have a zeptosecond script Lol... Sorry, I should have thought about just looking those up again.... Since I'm a idiot. How did you measure the time it took for that to process? Very simple: $dll=DLlOpen("ntdll.dll") $timer=TimerInit() _HighPrecisionSleep(2*1000,$dll); Sleep for 2 milliseconds. MsgBox(0,"",TimerDiff($timer)); If it says for example 2.35 then 350 microseconds was wasted. Just a question, why would you want to pause for a 100 nanoseconds? I can't see a real use for that much precision... But otherwise this is awesome! Actually, I just tested and 100 nanoseconds of sleep is enough to keep the CPU percentage at 0% on my machine And, Thanks Edited October 17, 2008 by monoceres Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
monoceres Posted August 9, 2008 Author Share Posted August 9, 2008 Wow that's interesting..this function is so unknown that this thread is already #6 when googling ZwDelayExecution Broken link? PM me and I'll send you the file! Link to comment Share on other sites More sharing options...
UPSman2 Posted August 9, 2008 Share Posted August 9, 2008 Ntdll.dll is an NT layer DLL file that controls Windows NT system functions.and the dll is full of functions Link to comment Share on other sites More sharing options...
andybiochem Posted August 9, 2008 Share Posted August 9, 2008 Hi Monoceres! This looks really good, I could do with an accurate sleep for a timer I'm working on. I don't seems to be able to get it to work....am I using it incorrectly?? : #include <array.au3> ;----- open dll ----- $dll = DllOpen("ntdll.dll") ;----- array of sleeps to test ----- Dim $sleep[6] = [0.1,1,10,50,100,500] ;----- cal HP-sleep for each ----- For $i = 0 to 5 $t = TimerInit() _HighPrecisionSleep($sleep[$i],$dll) $sleep[$i] &= " = " & TimerDiff($t) Next ;----- display array ----- _ArrayDisplay($sleep) ;----- hp sleep by monoceres ----- Func _HighPrecisionSleep($iMicroSeconds,$hDll=False) Local $hStruct, $bLoaded If Not $hDll Then $hDll=DllOpen("ntdll.dll") $bLoaded=True EndIf $hStruct=DllStructCreate("int64 time;") DllStructSetData($hStruct,"time",-1*($iMicroSeconds*10)) DllCall($hDll,"dword","ZwDelayExecution","int",0,"ptr",DllStructGetPtr($hStruct)) If $bLoaded Then DllClose($hDll) EndFunc ...I keep getting 15.x ms of sleep back??? - Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar! Link to comment Share on other sites More sharing options...
BrettF Posted August 9, 2008 Share Posted August 9, 2008 I'm thinking you should have: _HighPrecisionSleep($sleep[$i]*1000,$dll) Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
andybiochem Posted August 9, 2008 Share Posted August 9, 2008 Hmmm, maybe I'm not using it correctly??? expandcollapse popup;============================================= ; Testing 3 different types of SLEEP function ;============================================= #include <array.au3> ;----- results array ----- Dim $results[1] ;----- array of sleeps to test ----- Dim $sleep[6] = [0.1,1,10,50,100,500] ;----- call HP-sleep ----- _ArrayAdd($results,"_HighPrecisionSleep()") $dll = DllOpen("ntdll.dll") For $i = 0 to 5 $t = TimerInit() _HighPrecisionSleep($sleep[$i] * 1000,$dll) _ArrayAdd($results,$sleep[$i] & " = " & TimerDiff($t)) Next ;----- call accurate sleep by ludocus ----- _ArrayAdd($results,"") _ArrayAdd($results,"_AccurateSleep()") For $i = 0 to 5 $t = TimerInit() _AccurateSleep($sleep[$i]) _ArrayAdd($results,$sleep[$i] & " = " & TimerDiff($t)) Next ;----- call normal sleep ----- _ArrayAdd($results,"") _ArrayAdd($results,"AutoIt Sleep()") For $i = 0 to 5 $t = TimerInit() Sleep($sleep[$i]) _ArrayAdd($results,$sleep[$i] & " = " & TimerDiff($t)) Next ;----- display array ----- _ArrayDisplay($results) ;##### FUNCTIONS ##### ;----- hp sleep by monoceres ----- Func _HighPrecisionSleep($iMicroSeconds,$hDll=False) Local $hStruct, $bLoaded If Not $hDll Then $hDll=DllOpen("ntdll.dll") $bLoaded=True EndIf $hStruct=DllStructCreate("int64 time;") DllStructSetData($hStruct,"time",-1*($iMicroSeconds*10)) DllCall($hDll,"dword","ZwDelayExecution","int",0,"ptr",DllStructGetPtr($hStruct)) If $bLoaded Then DllClose($hDll) EndFunc ;----- accurate sleep by ludocus ----- Func _AccurateSleep($sTime) $timer=TimerInit() Do $tmp = TimerDiff($timer) Until $tmp >= $sTime EndFunc See my results: - Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar! Link to comment Share on other sites More sharing options...
BrettF Posted August 9, 2008 Share Posted August 9, 2008 My results: Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version! Link to comment Share on other sites More sharing options...
DexterMorgan Posted August 9, 2008 Share Posted August 9, 2008 This is pretty cool May come in handy somewhere code Link to comment Share on other sites More sharing options...
TomCat Posted January 21, 2009 Share Posted January 21, 2009 Hi have some problems with your _HighPrecisionSleep funktion. After a time the sleep time is getting longer and longer in a running script any idea why ? Link to comment Share on other sites More sharing options...
AdmiralAlkex Posted January 21, 2009 Share Posted January 21, 2009 Hi have some problems with your _HighPrecisionSleep funktion.After a time the sleep time is getting longer and longer in a running script any idea why ?_HighPrecisionSleep() shouldn't get longer no matter how many times you run it, please make a small (runnable) reproducer that shows the problem and I am sure the problem can be found .Some of my scripts: ShiftER, Codec-Control, Resolution switcher for HTC ShiftSome of my UDFs: SDL UDF, SetDefaultDllDirectories, Converting GDI+ Bitmap/Image to SDL Surface Link to comment Share on other sites More sharing options...
jvanegmond Posted February 28, 2009 Share Posted February 28, 2009 Thank you again, monoceres. Just for convenience I thought I post this: $hDll=DllOpen("ntdll.dll") $init = TimerInit() _HighPrecisionSleepUnsafe(1000000, $hDll) ConsoleWrite( TimerDiff($init) ) Func _HighPrecisionSleepUnsafe($iMicroSeconds,$hDll) _HighPrecisionSleepRawUnsafe( -1*($iMicroSeconds*10), $hDll ) EndFunc Func _HighPrecisionSleepRawUnsafe($iMicroSecondsRaw, $hDll) $hStruct=DllStructCreate("int64 time;") DllStructSetData($hStruct,"time",$iMicroSecondsRaw) DllCall($hDll,"dword","ZwDelayExecution","int",0,"ptr",DllStructGetPtr($hStruct)) EndFunc github.com/jvanegmond Link to comment Share on other sites More sharing options...
WolfWorld Posted March 1, 2009 Share Posted March 1, 2009 For people who want it to be more accurate try this Global $hDll=DllOpen("ntdll.dll") Func _HighPrecisionSleep($iMicroSeconds) Local $hStruct $hStruct=DllStructCreate("int64 time;") DllStructSetData($hStruct,"time",-1*($iMicroSeconds*10)) DllCall($hDll,"dword","ZwDelayExecution","int",0,"ptr",DllStructGetPtr($hStruct)) EndFunc This will be faster at least on my PC. Remember Autoit is still a scripting language. Main project - Eat Spaghetti - Obfuscate and Optimize your script. The most advance add-on.Website more of GadGets! 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