Core API - danielep71/VBA-PERFORMANCE_MANAGER GitHub Wiki

Core API

This page explains the main public API of cPerformanceManager and how the class is intended to be used in normal practice.

The core workflow is built around three members:

  • StartTimer
  • ElapsedSeconds
  • ElapsedTime

Core timing model

cPerformanceManager uses a session-bound timing model.

That means a timing operation is a small structured session:

  1. choose a timing backend
  2. start a timing session
  3. run the workload
  4. read the elapsed time
  5. optionally inspect state or diagnostics
  6. optionally capture checkpoints
  7. clean up environment changes

The three most important members

StartTimer

Starts a timing session and binds the selected method to that session.

cPM.StartTimer [iMethod], [AlignToNextTick]

Default method:

5

ElapsedSeconds

Returns the elapsed time as a numeric Double in seconds.

ElapsedS = cPM.ElapsedSeconds([iMethod])

Use this for:

  • benchmark values
  • comparisons
  • thresholds
  • numeric logging
  • aggregation

ElapsedTime

Returns the elapsed time as a human-readable string.

Debug.Print cPM.ElapsedTime([iMethod], [ElapsedSecondsIn])

It can also format an already measured numeric elapsed value without taking a second timing sample.

Additional public surfaces

Session / state inspection

  • T1 As Double
  • T2 As Double
  • ET As Double
  • ActiveMethodID As Integer
  • HasActiveSession As Boolean
  • MethodName(ByVal Idx As Integer) As String
  • StrictMode As Boolean

Execution control / environment

  • Pause(ByVal dSeconds As Double, Optional ByVal iMethod As Integer = 1)
  • ResetEnvironment()

Time-waster suppression

  • TW_Turn_OFF(Optional ByVal Except As TW_Enum = TW_Enum.None)
  • TW_Turn_ON()
  • TW_IsActive As Boolean
  • TW_ActiveSessionCount As Long

Diagnostics / benchmark helpers

  • OverheadMeasurement_Seconds
  • OverheadMeasurement_Text
  • Get_SystemTickInterval
  • QPC_Get_SystemTickInterval
  • QPC_FrequencyPerSecond
  • QPC_FrequencyPerSecond_Value

Structured checkpoint / reporting surface

  • SetRunLabel(ByVal RunLabel As String)
  • RunLabel As String
  • Checkpoint(ByVal CheckpointName As String, Optional ByVal NoteText As String = vbNullString)
  • CheckpointCount As Long
  • ClearCheckpoints()
  • ReportAsArray() As Variant
  • ReportAsText() As String

Normal calling sequence

Dim cPM         As cPerformanceManager
Dim ElapsedS    As Double

Set cPM = New cPerformanceManager

cPM.StartTimer 5

Range("A1:A10000").Value = 1

ElapsedS = cPM.ElapsedSeconds

Debug.Print ElapsedS

cPM.ResetEnvironment
Set cPM = Nothing

Why the class is session-bound

A timing session stores:

  • the active timing backend
  • the start timestamp
  • whether a session is active
  • internal state needed for elapsed evaluation
  • structured checkpoint / reporting state

This prevents accidental misuse such as starting with one backend and reading with another, and it keeps checkpoint data tied to one timing run.

Recommended safe pattern

Option Explicit

Public Sub Example_CoreAPI_SafePattern()

    Dim cPM As cPerformanceManager

    On Error GoTo CleanFail

    Set cPM = New cPerformanceManager

    cPM.StartTimer 5

    Worksheets(1).UsedRange.Calculate

    Debug.Print "Elapsed: " & cPM.ElapsedTime

CleanExit:
    If Not cPM Is Nothing Then
        cPM.ResetEnvironment
        Set cPM = Nothing
    End If
    Exit Sub

CleanFail:
    Debug.Print "Error " & Err.Number & " - " & Err.Description
    Resume CleanExit

End Sub

For structured reporting examples, see Checkpoint and Reporting.