nullschritt

_microseconds() get system microseconds

3 posts in this topic

I needed this functionality for some high precision calculations I'm making. (autoit is not fast enough to support counting nanoseconds, sorry!)

What's it do?

It returns the number of microseconds past the current millisecond on the system clock.

How accurate is it?

Due to autoit limitations the counter will always be between 3-15 microseconds behind the system.

->*(This is how long autoit takes to initialize a counter)

Example + UDF:

_microsecinit() ;we HAVE to call this first to initialize the the counter

while 1
    sleep(random(25,100)) ;random sleep to show microsecond difference better
    ConsoleWrite(@yday&'/'&@year&' '&@HOUR&':'&@MIN&':'&@SEC&':'&@MSEC&':'&_microseconds()&@CRLF) ;call _microseconds() to get the microseconds
WEnd


Func _microseconds()
    local $ms, $ms2
    if TimerDiff($microtimer) > 5000 Then
        $ms = @MSEC
        $ms2 = $ms
        Do
            $ms2 = @MSEC
        Until $ms2 > $ms
        $microtimer = TimerInit() ;attempt to re-sync microseconds
    EndIf
    $time = StringSplit(TimerDiff($microtimer), ".")
    $time = StringLeft($time[2], 4)
    return $time
EndFunc

func _microsecinit()
local $ms, $ms2
$ms = @MSEC
$ms2 = $ms
Do
    $ms2 = @MSEC
Until $ms2 > $ms
global $microtimer = TimerInit()
EndFunc

If you have any ways of making it more precise, please submit it!

Share this post


Link to post
Share on other sites



Also for anyone interested, we can also get nanoseconds.

Func _nanotimer()
        local $ms, $ms2
    if TimerDiff($microtimer) > 5000 Then
        $ms = @MSEC
        $ms2 = $ms
        Do
            $ms2 = @MSEC
        Until $ms2 > $ms
        $microtimer = TimerInit() ;attempt to re-sync microseconds
    EndIf
    $time = StringSplit(TimerDiff($microtimer), ".")
    $time = stringtrimleft(StringLeft($time[2], 8), 4)
    return $time
EndFunc

Share this post


Link to post
Share on other sites
On 2/6/2014 at 1:44 AM, nullschritt said:

I needed this functionality for some high precision calculations I'm making. (autoit is not fast enough to support counting nanoseconds, sorry!)

What's it do?

It returns the number of microseconds past the current millisecond on the system clock.

How accurate is it?

Due to autoit limitations the counter will always be between 3-15 microseconds behind the system.

->*(This is how long autoit takes to initialize a counter)

Example + UDF:

_microsecinit() ;we HAVE to call this first to initialize the the counter

while 1
    sleep(random(25,100)) ;random sleep to show microsecond difference better
    ConsoleWrite(@yday&'/'&@year&' '&@HOUR&':'&@MIN&':'&@SEC&':'&@MSEC&':'&_microseconds()&@CRLF) ;call _microseconds() to get the microseconds
WEnd


Func _microseconds()
    local $ms, $ms2
    if TimerDiff($microtimer) > 5000 Then
        $ms = @MSEC
        $ms2 = $ms
        Do
            $ms2 = @MSEC
        Until $ms2 > $ms
        $microtimer = TimerInit() ;attempt to re-sync microseconds
    EndIf
    $time = StringSplit(TimerDiff($microtimer), ".")
    $time = StringLeft($time[2], 4)
    return $time
EndFunc

func _microsecinit()
local $ms, $ms2
$ms = @MSEC
$ms2 = $ms
Do
    $ms2 = @MSEC
Until $ms2 > $ms
global $microtimer = TimerInit()
EndFunc

If you have any ways of making it more precise, please submit it!

#include <WinAPISys.au3>
$c = _WinAPI_QueryPerformanceFrequency()
$a = _WinAPI_QueryPerformanceCounter()
sleep(1000)
$b = _WinAPI_QueryPerformanceCounter()
MsgBox(0, "Time in seconds", (($b - $a) / $c))

Moving the decimal to the right 6 places will yield microseconds

Quote
#include <WinAPISys.au3>
$c = _WinAPI_QueryPerformanceFrequency()
$a = _WinAPI_QueryPerformanceCounter()
$b = _WinAPI_QueryPerformanceCounter()
MsgBox(0, "Microseconds", (($b - $a) / $c)*1000000)

 

Thought I would throw my 2 cents in with a simpler method.

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