QPC() - Stopwatch timing

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

QPC() - Stopwatch timing

01 Sep 2014, 18:18

#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
My Scripts and Functions: V1  V2
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: QPC() - Stopwatch timing

01 Sep 2014, 18:36

What are the advantages over QPX() other than #warn? Or is it more like an update??
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: QPC() - Stopwatch timing

01 Sep 2014, 19:26

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.
User avatar
joedf
Posts: 8953
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: QPC() - Stopwatch timing

01 Sep 2014, 19:40

Ok I see. And, Thanks for the Win8 function! ;)
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: QPC() - Stopwatch timing

02 Sep 2014, 00:51

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.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: QPC() - Stopwatch timing

07 Sep 2021, 07:24

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.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: QPC() - Stopwatch timing

07 Sep 2021, 10:11

Hi @jNizM
Why are you enforcing .7f?
Format() or Round(), it would be better to do it outside the function.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

[V2] QPC() - Stopwatch timing

07 Sep 2021, 10:12

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) )
My Scripts and Functions: V1  V2
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: QPC() - Stopwatch timing

08 Sep 2021, 00:43

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.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: QPC() - Stopwatch timing

08 Sep 2021, 00:48

Btw. I found an interesting article about QPC with HPET and QPC with TSC. But it is in german.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble and 144 guests