Code: Select all
;/////////////////////////////////////////////////////////////////////////////////////////////
;
; Module: Stopwatch.pbi
; Author: Lloyd Gallant (netmaestro) www.lloydsplace.com
; Date: August 4, 2016
; Target Compiler: PureBasic 5.50
; License: Do as you like with it, no restrictions and NO warranty
;
; What is it: Quick-and-dirty hires timer with automatic result display,
; designed for minimal user interaction. Returns precise milliseconds.
; Feel free to modify it for more complex or user-specific tasks.
;
; Commands: StartStopwatch -Clears and starts the timer
; ReadStopwatch -Reads the timer value and displays it
;
; Usage example appears after the code. (May be left intact if
; using as an include file)
;
; If you're precision-timing code execution, as always, remember
; to turn the debugger off. (DisableDebugger in code is not sufficient)
;
;////////////////////////////////////////////////////////////////////////////////////////////
DeclareModule Stopwatch
Structure SWLARGEINTEGER
StructureUnion
Longs.LARGE_INTEGER
QuadPart.q
EndStructureUnion
EndStructure
#SW_Begin = 0
#SW_End = 1
Declare.l CallStopwatch(mode)
Macro StartStopwatch
Stopwatch::CallStopwatch(Stopwatch::#SW_Begin)
EndMacro
Macro ReadStopwatch
MessageRequester("Stopwatch", Str(Stopwatch::CallStopwatch(Stopwatch::#SW_End)))
EndMacro
EndDeclareModule
Module Stopwatch
Procedure.l CallStopwatch(mode)
Static.SWLARGEINTEGER StartingTime, Frequency
Define.SWLARGEINTEGER EndingTime, ElapsedMicroseconds
Select mode
Case #SW_Begin
QueryPerformanceFrequency_(@Frequency)
QueryPerformanceCounter_(@StartingTime)
Case #SW_End
QueryPerformanceCounter_(@EndingTime)
ElapsedMicroseconds\QuadPart = EndingTime\QuadPart - StartingTime\QuadPart
ElapsedMicroseconds\QuadPart = ElapsedMicroseconds\QuadPart * 1000
ElapsedMicroseconds\QuadPart = ElapsedMicroseconds\QuadPart / Frequency\QuadPart
ProcedureReturn ElapsedMicroseconds\Longs\lowpart
EndSelect
EndProcedure
EndModule
CompilerIf #PB_Compiler_IsMainFile
Stopwatch::StartStopwatch
Delay(1000)
Stopwatch::ReadStopwatch
CompilerEndIf