Another Module conversion, this time a HighResTimer.
Best regards
StarBootics
Code: Select all
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : HighResTimer - Module
; File Name : HighResTimer - Module.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 06-08-2016
; Last Update : 06-08-2016
; PureBasic code : V5.50
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; This code was originally created by Guimauve to have a High Resolution
; Timer. See : http://www.purebasic.fr/english/viewtopic.php?f=15&t=49365
;
; I deserve credit only to convert the original code into a Module.
;
; This code is free to be use where ever you like but you use it at your
; own risk.
;
; The author can in no way be held responsible for data loss, damage or
; other annoying situations that may occur.
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
DeclareModule HighResTimer
Declare Initialize()
Declare Reset()
Declare Start()
Declare Stop()
Declare.q Consult()
EndDeclareModule
Module HighResTimer
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
#CLOCK_MONOTONIC = 1
Structure TimeSpec Align #PB_Structure_AlignC
Second.i
NanoSecond.l
EndStructure
ImportC "-lrt"
clock_gettime(ClockID.l, *TimeSpecA.TimeSpec)
EndImport
CompilerCase #PB_OS_MacOS
Structure TimeBase Align #PB_Structure_AlignC
Numerator.l
Denominator.l
EndStructure
ImportC ""
mach_absolute_time.q()
mach_timebase_info(*TimeBaseA.TimeBase)
EndImport
CompilerEndSelect
Structure Instance Align #PB_Structure_AlignC
Stopped.b
StartMicroSec.q
StopMicroSec.q
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
Frequency.q
StartCount.q
EndCount.q
CompilerCase #PB_OS_Linux
StartCount.TimeSpec
EndCount.TimeSpec
CompilerCase #PB_OS_MacOS
MicroSecConversion.d
TimeBase.TimeBase
StartCount.q
EndCount.q
CompilerEndSelect
EndStructure
Global Instance.Instance
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The New Operator <<<<<
Procedure Initialize()
Instance\Stopped = #True
Instance\StartMicroSec = 0
Instance\StopMicroSec = 0
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
QueryPerformanceFrequency_(@Instance\Frequency)
Instance\StartCount = 0
Instance\EndCount = 0
CompilerCase #PB_OS_Linux
Instance\StartCount\Second = 0
Instance\StartCount\NanoSecond = 0
Instance\EndCount\Second = 0
Instance\EndCount\NanoSecond = 0
CompilerCase #PB_OS_MacOS
mach_timebase_info(@Instance\TimeBase)
Instance\MicroSecConversion = 1e-3 * Instance\TimeBase\Numerator / Instance\TimeBase\Denominator
Instance\StartCount = 0
Instance\EndCount = 0
CompilerEndSelect
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Reset Operator <<<<<
Procedure Reset()
Instance\Stopped = 0
Instance\StartMicroSec = 0
Instance\StopMicroSec = 0
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
Instance\Frequency = 0
Instance\StartCount = 0
Instance\EndCount = 0
CompilerCase #PB_OS_Linux
Instance\StartCount\Second = 0
Instance\StartCount\NanoSecond = 0
Instance\EndCount\Second = 0
Instance\EndCount\NanoSecond = 0
CompilerCase #PB_OS_MacOS
Instance\MicroSecConversion = 0.0
Instance\TimeBase\Numerator = 0
Instance\TimeBase\Denominator = 0
Instance\StartCount = 0
Instance\EndCount = 0
CompilerEndSelect
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Start operator <<<<<
Procedure Start()
Instance\Stopped = #False
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
QueryPerformanceCounter_(@Instance\StartCount)
CompilerCase #PB_OS_Linux
clock_gettime(#CLOCK_MONOTONIC, @Instance\StartCount)
CompilerCase #PB_OS_MacOS
Instance\StartCount = mach_absolute_time() * Instance\MicroSecConversion
CompilerEndSelect
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Stop operator <<<<<
Procedure Stop()
Instance\Stopped = #True
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
QueryPerformanceCounter_(@Instance\EndCount)
CompilerCase #PB_OS_Linux
clock_gettime(#CLOCK_MONOTONIC, @Instance\EndCount)
CompilerCase #PB_OS_MacOS
Instance\EndCount = mach_absolute_time() * Instance\MicroSecConversion
CompilerEndSelect
EndProcedure
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<< The Consult Operator <<<<<
Procedure.q Consult()
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
If Instance\Stopped = #False
QueryPerformanceCounter_(@Instance\EndCount)
EndIf
Instance\StartMicroSec = Instance\StartCount * (1000000.0 / Instance\Frequency)
Instance\StopMicroSec = Instance\EndCount * (1000000.0 / Instance\Frequency)
CompilerCase #PB_OS_Linux
If Instance\Stopped = #False
clock_gettime(#CLOCK_MONOTONIC, @Instance\EndCount)
EndIf
Instance\StartMicroSec = Instance\StartCount\Second * 1000000 + Instance\StartCount\NanoSecond / 1000
Instance\StopMicroSec = Instance\EndCount\Second * 1000000 + Instance\EndCount\NanoSecond / 1000
CompilerCase #PB_OS_MacOS
If Instance\Stopped = #False
Instance\EndCount = mach_absolute_time() * Instance\MicroSecConversion
EndIf
Instance\StartMicroSec = Instance\StartCount
Instance\StopMicroSec = Instance\EndCount
CompilerEndSelect
ProcedureReturn Instance\StopMicroSec - Instance\StartMicroSec
EndProcedure
EndModule
CompilerIf #PB_Compiler_IsMainFile
HighResTimer::Initialize() ; <-- Mandatory to Initialize the timer before using it
HighResTimer::Start()
Delay(1)
Debug HighResTimer::Consult()
Delay(5)
Debug HighResTimer::Consult()
Delay(499)
Debug HighResTimer::Consult()
Delay(500)
Debug HighResTimer::Consult()
Delay(1499)
Debug HighResTimer::Consult()
Delay(1501)
Debug HighResTimer::Consult()
HighResTimer::Stop()
HighResTimer::Reset()
CompilerEndIf
; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<