Sleep down to 100 Nano seconds

Recommended Posts

monoceres 183

monoceres 183

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.

Share this post

Link to post

Share on other sites

martin 66

martin 66

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.)

Share this post

Link to post

Share on other sites

monoceres 183

monoceres 183

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).

Share this post

Link to post

Share on other sites

Szhlopp 15

Szhlopp 15

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).