Page 1 of 1

QPC() - Stopwatch timing

Posted: 01 Sep 2014, 18:18
by SKAN
#Warn was complaining about uninitialized variables in QPX(). Instead of fixing it, I ended up with a simplified version.

QPC( Reset )
Reset = True : Resets the counter ( and returns the number of seconds since the computer was rebooted ).
Reset = False : Returns the number of seconds elapsed since last reset.

Code: Select all

QPC( R := 0 ) {    ; By SKAN,  http://goo.gl/nf7O4G,  CD:01/Sep/2014 | MD:01/Sep/2014
  Static P := 0,  F := 0,     Q := DllCall( "QueryPerformanceFrequency", "Int64P",F )
Return ! DllCall( "QueryPerformanceCounter","Int64P",Q ) + ( R ? (P:=Q)/F : (Q-P)/F ) 
}
Verbose code

Basic usage example:

Code: Select all

QPC( True )        ; Reset counter
Sleep 1000
Ti := QPC( False ) ; Retrieve time consumed since last reset

MsgBox, 0, Sleep 1000, %Ti% seconds
Test code: Stopwatch. Lap timing vs Split timing

Code: Select all

QPC( True )

; do something
Sleep 234

Lap1 := QPC( False ),  QPC( True )

; do something
Sleep 234

Lap2 := QPC( False ),  QPC( True )

; do something
Sleep 234

Lap3 := QPC( False )

MsgBox % "Lap 1`t" Lap1 "`nLap 2`t" Lap2 "`nLap 3`t" Lap3 "`n`nTotal`t" Lap1 + Lap2 + Lap3


QPC( True )

; do something
Sleep 234

Split1 := QPC( False )

; do something
Sleep 234

Split2 := QPC( False )

; do something
Sleep 234

Split3 := QPC( False )

MsgBox % "Split 1`t" Split1 "`nSplit 2`t" Split2 "`nSplit 3`t" Split3 "`n`nFinal`t" Split3

Re: QPC() - Stopwatch timing

Posted: 01 Sep 2014, 18:36
by joedf
What are the advantages over QPX() other than #warn? Or is it more like an update??

Re: QPC() - Stopwatch timing

Posted: 01 Sep 2014, 19:26
by SKAN
While % QPX( n ) feature is buggy and should have never existed in the first place. You can read Lexikos' comments on that topic.

Windows 8 onwards, there is no need an alternate for QPC, as a new function GetSystemTimePreciseAsFileTime() providing similar facility.

Code: Select all

t1 := t2 := 0
DllCall( "GetSystemTimePreciseAsFileTime", "Int64P",t1 )
Sleep 1000
DllCall( "GetSystemTimePreciseAsFileTime", "Int64P",t2 )

MsgBox % ( t2 - t1 ) "us`n"  ( t2 - t1 ) / 10000000 "s"

/*
---------------------------
GetSystemTimePreciseAsFileTime.ahk
---------------------------
9996941us
0.999694s
---------------------------
OK   
---------------------------
*/
I have removed the redundant code [ which I never used after Lexikos' comment ] and made the function simpler.

Re: QPC() - Stopwatch timing

Posted: 01 Sep 2014, 19:40
by joedf
Ok I see. And, Thanks for the Win8 function! ;)

Re: QPC() - Stopwatch timing

Posted: 02 Sep 2014, 00:51
by jNizM
GetSystemTimePreciseAsFileTime

Note: This function is best suited for high-resolution time-of-day measurements, or time stamps that are synchronized to UTC. For high-resolution interval measurements, use QueryPerformanceCounter or KeQueryPerformanceCounter. For more info about acquiring high-resolution time stamps, see Acquiring high-resolution time stamps.

Re: QPC() - Stopwatch timing

Posted: 07 Sep 2021, 07:24
by jNizM
Just needed it for a measurement. Small v2 rewrite:

Code: Select all

QPC(reset := 0)
{
	static frq := 0, qpc := 0, pqpc := 0

	if !(frq)
		DllCall("kernel32\QueryPerformanceFrequency", "int64*", &frq := 0)
	DllCall("kernel32\QueryPerformanceCounter", "int64*", &qpc := 0)

	if (reset)
		pqpc := qpc
	else
		return Format("{:.7f}", (qpc - pqpc) / frq)
}
Used Format because of: https://lexikos.github.io/v2/docs/Concepts.htm#pure-numbers
And with .7f, because in my tests everything with the 8th decimal place was filled with 0.

Re: QPC() - Stopwatch timing

Posted: 07 Sep 2021, 10:11
by SKAN
Hi @jNizM
Why are you enforcing .7f?
Format() or Round(), it would be better to do it outside the function.

[V2] QPC() - Stopwatch timing

Posted: 07 Sep 2021, 10:12
by SKAN

Code: Select all

QPC( R := 0 )  ;   By SKAN for ah2 on CT91/D497 @ goo.gl/nf7O4G
{ 
    Static P := 0,  F := 0, Q := DllCall("kernel32.dll\QueryPerformanceFrequency", "int64p",&F)
    Return( DllCall("kernel32.dll\QueryPerformanceCounter","int64p",&Q) * 0 + (R ? (P:=Q)*0 : (Q-P)/F) )
}

#Requires AutoHotkey v2.0-
#Warn
#SingleInstance

Qpc(1)
, Sleep(100)
, tt := Qpc()

MsgBox( Round(tt,3) )

Re: QPC() - Stopwatch timing

Posted: 08 Sep 2021, 00:43
by jNizM
The reason I use it in the function is that it might scare users off because the result looks like this:

Code: Select all

2.7500000000000001e-05
0.00071719999999999998
1.1000000000000001e-06
3.6900000000000002e-05
3.2499999999999997e-05
2.12e-05
0.069056199999999998
2.5923091
0.0150974
0.0021562
4.6600000000000001e-05
0.0045818999999999999
With Format .7f it looks like this:

Code: Select all

0.0000282
0.0007551
0.0000012
0.0000388
0.0000344
0.0000218
0.0690411
2.4945419
0.0114842
0.0019123
0.0000454
0.0041426
But yes it can also be used as function parameter Format("{:. " precision "f}", ...).
I used 7 because it is the highest precision. After 7 it is always 0.

But it can also be used outside of the function like you did. I prefered for my test inside because Format is just called with the "stop" paramter and dont want to used it multiple times in my test.

Re: QPC() - Stopwatch timing

Posted: 08 Sep 2021, 00:48
by jNizM
Btw. I found an interesting article about QPC with HPET and QPC with TSC. But it is in german.