QPC() - Stopwatch timing

Post your working scripts, libraries and tools
User avatar
SKAN
Posts: 367
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
User avatar
joedf
Posts: 7131
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
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 @ 4.00 GHz, 16GB DDR4 2733 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
User avatar
SKAN
Posts: 367
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: 7131
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
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 @ 4.00 GHz, 16GB DDR4 2733 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
User avatar
jNizM
Posts: 2435
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
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] 1.1.30.03 x64 Unicode | [WIN] 10 Pro (Version 1903) x64 | [GitHub] Profile
Donations are appreciated if I could help you

Return to “Scripts and Functions”

Who is online

Users browsing this forum: 112, robodesign and 30 guests