PerformanceTimer – A class module for high-resolution time measurement

'-------------------------------------------' PerformanceTimer class module'-------------------------------------------' Use this class to profile your code and any other operation' typically with a precision greater than 1 millionth of a second'' As soon as you create an object, the timer starts' but you can also start it explicitly with StartTimer' Stop the timer and retrieve timing with StopTimer, or' get the timing without stopping the timer with ElapsedTime'' The TotalTime property returns the number of seconds the' timer has been active, so you can use it to sum up partial' timings, after swithing the timer on and off' The FormatTime is similar to elapsed time, but returns' the time as a formatted string with desired precision'' Example:'         Dim pc As New PerformanceCounter'         pc.StartTimer'         ' ...'         ' put here the code you want to benchmark'         ' ...'         ' print elapsed time, but don't stop the timer'         Debug.Print pc.ElapsedTime'         ' ...'         ' so something else here'         ' ...'         ' print elapsed time and stop the timer'         Debug.Print pc.StopTimer'         ' ...'         ' prepare another benchmark here'         ' ...'         ' start the benchmark, without resetting total time'         pc.StartTimer'         ' ...'         ' put here the code you want to benchmark'         ' ...'         ' print elapsed as a formatted string'         Debug.Print pc.FormatTime("Second benchmark ### secs.", 4)'         ' print total time'         Debug.Print pc.TotalTime'Option ExplicitPrivate Declare Function QueryPerformanceFrequencyAny Lib "kernel32" Alias _    "QueryPerformanceFrequency" (lpFrequency As Any) As LongPrivate Declare Function QueryPerformanceCounterAny Lib "kernel32" Alias _    "QueryPerformanceCounter" (lpPerformanceCount As Any) As Long' the frequency for this computerDim frequency As CurrencyDim startTime As CurrencyDim endTime As CurrencyDim totTime As Currency' Start the timer'' if argument is True, it also resets the' internal total time counterSub StartTimer(Optional ByVal ResetTotalTime As Boolean)    ' get the current value of the counter    QueryPerformanceCounterAny startTime    ' reset total time counter if requested    If ResetTotalTime Then totTime = 0End Sub' stop the timer'' returns the time elapsed since StartTimerFunction StopTimer() As Double    ' get the elapsed time    StopTimer = ElapsedTime    ' update the total time counter    totTime = totTime + (endTime - startTime)    ' reset starting time    startTime = 0End Function' return the elapsed time in seconds since StartTimer' without stopping the timerProperty Get ElapsedTime() As Double    ' exit if StartTimer hasn't been called since    ' the previous call to StopTimer    If startTime = 0 Then Exit Property        ' get the current value of the counter    QueryPerformanceCounterAny endTime    ' return the elapsed time in seconds    ElapsedTime = (endTime - startTime) / frequencyEnd Property' return the total time in secondsProperty Get TotalTime() As Double    If startTime = 0 Then        ' StopTimer has been called        ' so totTime is correctly updated        TotalTime = totTime / frequency    Else        TotalTime = (totTime + (endTime - startTime)) / frequency    End IfEnd Property' return a time value as a formatted string' if second argument is omitted, it uses ElapsedTime'' return it as a formatted string with' specified number of decimal - use ### in the string' as a placeholder for the elapsed time'   e.g.  Print GetTimeMsg("Elapsed ### secs.", , 4)'' NOTE: this function is slightly less precise than'       GetTime, because arguments are passedProperty Get FormatTime(msg As String, Optional seconds As Double = -1, _    Optional ByVal decDigits As Integer = 7) As String    ' get the elapsed time if not passed as an argument    If seconds < 0 Then seconds = ElapsedTime()    ' build the result string    FormatTime = Replace(msg, "###", CStr(Round(seconds, decDigits)))End Property' return the timer precision in secondsProperty Get Precision() As Double    ' frequency must be scaled up by 10E4    Precision = 1 / (frequency * 10000#)End Property' evaluate the frequency once and for all' when this object is createdPrivate Sub Class_Initialize()    ' raise error if API functions aren't supported    If QueryPerformanceFrequencyAny(frequency) = 0 Then        Err.Raise 1001, , "This system doesn't support high-res timing"    End If    ' get start time as well    StartTimerEnd Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.