Super simple stopwatch with DllCall, not with "tickcount"

Post your working scripts, libraries and tools for AHK v1.1 and older
neokix
Posts: 36
Joined: 10 May 2017, 07:50
Contact:

Super simple stopwatch with DllCall, not with "tickcount"

19 Aug 2017, 03:26

*For more improved one, see those following posts. (added in 2017/08/20)

I am just a super beginner and I wanted to make simple stopwatch script with "DllCall", not with "tickcount".
But as I searched with my poor English, I couldn't find any. So here it is, the super simple stopwatch, for super beginners like me.
F3 for start, F4 for stop and send the time between them as text.

Code: Select all

;Precise? Timer
F3::
CounterBefore =  ;var
DllCall("QueryPerformanceCounter", "Int64*", CounterBefore)
return

F4::
CounterAfter = ; var
DllCall("QueryPerformanceCounter", "Int64*", CounterAfter)
Send, {(}
Counterresult := (CounterAfter - CounterBefore)/2525000 
Send, % Round(Counterresult, 1) ; rounds up the number
Send, {)}{Right}
return
As far as I know, this mysterious number "2525000" depends on system or BIOS or something (please post the comment and let me know if you have any knowledge about this), so, you have to scale it with the following script.

Code: Select all

F6::
CountBefore =
DllCall("QueryPerformanceCounter", "Int64*", CounterBefore)
Sleep, 1000
CounterAfter =
DllCall("QueryPerformanceCounter", "Int64*", CounterAfter)
Msgbox, % (CounterAfter - CounterBefore)
return
Those mysterious number will vary, so you have to use the average number to get the "that will do" number.
Also, I need detailed info here, if you have one.

But anyhow, this will do something, and can get a bit more constant result than the "tickcount" function, so yeah....
Last edited by neokix on 19 Aug 2017, 10:46, edited 1 time in total.
neokix
Posts: 36
Joined: 10 May 2017, 07:50
Contact:

Re: Super simple stopwatch with DllCall, not with "tickcount"

19 Aug 2017, 10:41

>just me
Thanks man, that'll be helpful.

So, calculation for that frequency can be...

Code: Select all

F6::
CountFrequency =
DllCall("QueryPerformanceFrequency", "Int64*", CountFrequency)
Msgbox, % CountFrequency
return
and maybe I can do something like this.
In this case, people don't have to tweak that mysterious frequency one.

Code: Select all

F3::
CounterBefore =  ;var1
DllCall("QueryPerformanceCounter", "Int64*", CounterBefore)
return

F4::
CounterAfter = ; var2
CountFrequency = ; var3
DllCall("QueryPerformanceFrequency", "Int64*", CountFrequency)
DllCall("QueryPerformanceCounter", "Int64*", CounterAfter)
Send, {(}
Counterresult := (CounterAfter - CounterBefore)/CountFrequency
Send, % Round(Counterresult, 1)
Send, {)}{Right}
return
This is not most beautiful thing on Earth but it is much better now!
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Super simple stopwatch with DllCall, not with "tickcount"

19 Aug 2017, 11:29

There are many implementations of these functions available. When put in a AHK custom function you can do simple calls if you like:

Code: Select all

F3:: CounterBefore := QPC()
F4:: MsgBox, % QPC() - CounterBefore


;-------------------------------------------------------------------------------
QPC() { ; microseconds precision
;-------------------------------------------------------------------------------
    static Freq, init := DllCall("QueryPerformanceFrequency", "Int64P", Freq)
    DllCall("QueryPerformanceCounter", "Int64P", Count)
    Return, Count / Freq
}
neokix
Posts: 36
Joined: 10 May 2017, 07:50
Contact:

Re: Super simple stopwatch with DllCall, not with "tickcount"

19 Aug 2017, 21:08

>wolf_II
Wow that's much simpler and beautiful. Thanks for the insight!

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble and 151 guests