Sign in to follow this  
Followers 0
Jex

About TimerDiff

7 posts in this topic

#1 ·  Posted (edited)

I'm curious how TimerDiff calculate time. I'm found using that operation :

$Test = TimerInit()
Sleep(500)
$Test2 = (TimerInit() - $Test) / ((TimerInit() - $Test) / TimerDiff($Test))
MsgBox("", "", $Test2)

With that method my functions can find TimerDiff but i'm curious what is 341..... :)

and my second question is TimerInit return which value? How can i get that value without use that function. ( Example dllcall maybe? )

Edited by Jex

Share this post


Link to post
Share on other sites



$t1 = TimerInit()

$t2 = 0
$a = DllCall('Kernel32.dll', 'int', 'QueryPerformanceCounter', 'int64*', $t2)
$t2 = $a[1]

MsgBox(0, '', $t1 & @CRLF & $t2)


"be smart, drink your wine"

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks Siao.

GaryFrost gave QueryPerformanceCounter in here http://www.autoitscript.com/forum/index.ph...st&p=457702

but i'm didn't understood what is that. I'm found that GetTickCount function : http://msdn2.microsoft.com/en-us/library/ms724408.aspx which will be better for create timer?

Edit:

I'm added 3 timer for test accuracy but QueryPerformanceCounter not give true result :

$Timer = TimerI(1)
$Timer2 = TimerI(2)
$Timer3 = TimerInit()
Sleep(534)
MsgBox("", "", "Timer : " & TimerD($Timer, 1) & @CRLF & "Timer2 : " & TimerD($Timer2, 2) & @CRLF & "Timer3 : " & TimerDiff($Timer3))

Func TimerI($tType = 1)
    If $tType = 1 Then
        $cTimer = DllCall("Kernel32.dll", "int", "GetTickCount")
        $rTimer = $cTimer[0]
    Else
        $cTimer = DllCall("Kernel32.dll", "int", "QueryPerformanceCounter", "int64*", "")
        $rTimer = $cTimer[1] / 3410000
    EndIf
    Return $rTimer
EndFunc

Func TimerD($gTimer, $tType = 1)
    If $tType = 1 Then
        $cTimer = DllCall("Kernel32.dll", "int", "GetTickCount")
        $rTimer = $cTimer[0]
    Else
        $cTimer = DllCall("Kernel32.dll", "int", "QueryPerformanceCounter", "int64*", "")
        $rTimer = $cTimer[1] / 3410000
    EndIf
    Return $rTimer - $gTimer
EndFunc

Ok now working i'm added "/ 341" .

Why i need divide to 341 ( or 3410000 ) for true result ?

Edited by Jex

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

GaryFrost gave QueryPerformanceFrequency in here http://www.autoitscript.com/forum/index.ph...st&p=457702

but i'm didn't understood what is that again. :)

Now timers working thanks.

TimerI(1) = QueryPerformanceCounter & QueryPerformanceFrequency

TimerI(2) = GetTickCount

TımerI(3) = TimerInit & TimerDiff

$Timer = TimerI(1)
Sleep(534)
$Timer = TimerD($Timer, 1)

$Timer2 = TimerI(2)
Sleep(534)
$Timer2 = TimerD($Timer2, 2)

$Timer3 = TimerI(3)
Sleep(534)
$Timer3 = TimerD($Timer3, 3)

MsgBox("", "", "Timer : " & $Timer & @CRLF & "Timer2 : " & $Timer2 & @CRLF & "Timer3 : " & $Timer3)

Func TimerI($tType = 1)
    If $tType = 1 Then
        $cTimer = DllCall("Kernel32.dll", "int", "QueryPerformanceCounter", "int64*", "")
        Return $cTimer[1]
    ElseIf $tType = 2 Then
        $cTimer = DllCall("Kernel32.dll", "int", "GetTickCount")
        Return $cTimer[0]
    Else
        Return TimerInit()
    EndIf
EndFunc   ;==>TimerI

Func TimerD($gTimer, $tType = 1)
    If $tType = 1 Then
        $cTimer = DllCall("Kernel32.dll", "int", "QueryPerformanceCounter", "int64*", "")
        $fTimer = DllCall("Kernel32.dll", "int", "QueryPerformanceFrequency", "int64*", "")
        Return 1000 * ($cTimer[1] - $gTimer) / $fTimer[1]
    ElseIf $tType = 2 Then
        $cTimer = DllCall("Kernel32.dll", "int", "GetTickCount")
        Return $cTimer[0] - $gTimer
    Else
        Return TimerDiff($gTimer)
    EndIf
EndFunc   ;==>TimerD
Edited by Jex

Share this post


Link to post
Share on other sites

TimerDiff() returns the number of ticks since last boot time.

Share this post


Link to post
Share on other sites

Something I wrote a while back..

;====================================================================
; How to simulate the Timer functions in Autoit using DLL calls
;====================================================================

Global $starttime = _TimerInit() 
While 1
    ToolTip(_TimerDiff($starttime))
WEnd

;====================================================================
; Functions in Autoit
;    TimerInit() 
;    TimerDiff()
; Functions in kernal32.dll
;    _QueryPerformanceFrequency()
;    _QueryPerformanceCounter()
;====================================================================

Func _TimerInit()
    Local $var = DllStructCreate("int64")
    DllCall("kernel32.dll", "int", "QueryPerformanceCounter", "ptr", DllStructGetPtr($var, 1))
    Local $starttime = DllStructGetData($var, 1)
    $startvar = 0
    Return $starttime
EndFunc

Func _TimerDiff($timer)
    Return 1000 * ( _QueryPerformanceCounter() - $timer ) / _QueryPerformanceFrequency()
EndFunc

Func _QueryPerformanceFrequency()
    Local $var = DllStructCreate("int64")
    DllCall("kernel32.dll", "int", "QueryPerformanceFrequency", "ptr", DllStructGetPtr($var, 1))
    Local $returnval = DllStructGetData($var, 1)
    $var = 0
    Return $returnval
EndFunc

Func _QueryPerformanceCounter()
    Local $var = DllStructCreate("int64")
    DllCall("kernel32.dll", "int", "QueryPerformanceCounter", "ptr", DllStructGetPtr($var, 1))
    Local $returnval = DllStructGetData($var, 1)
    $var = 0
    Return $returnval
EndFunc

www.itoady.com

A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding

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