VBcoders Browse New Submit Contact Sign In

No account? Register free

Forgot password?

DebugTimer

Matthew Heydman  (2 Submissions)   Debugging and Error Handling   Visual Basic 3.0   Unknown Difficulty   Wed 3rd February 2021

Have you ever been asked: Which part of the routine is taking so long? or did you ever wonder what function was bogging your app down, or did you ever just want to time a particular statement or function? Welcome to DebugTimer. It's not a resource hog and uses no active-x controls... just the built-in Timer function in VB. This is a very easily implemented class module that allows you to time any line(s) of code or functions or whatever. You can even use multiple timers or nest them. I wrote this to determine the length of time it took to perform various stored procedures, and it worked great. If you
have a similar need, I'm sure this will do the trick.

API Declarations
Add a new class module to your project, and name it clsDebugTimer. Paste the following code into it:

' METHODS:
'
' Begin(nTimerIndex, nTimerDescription)
' - Starts/resets a new timer. Both parameters are optional.
'
' nTimerIndex should be a number from 0 to 9 to specify
' which timer is to be used. Omitting this param is the same
' as passing a zero as this parameter.
'
' nTimerDescription is a description which can be anything you
' like, but should probably describe what it is you are timing.
' Omitting this param will set the description to "Timer 1" (or
' whatever time index you are using instead of 1)
'
'
' ShowElapsed(nOutputType, nTimerIndex)
' -Displays the elasped time for the timer specified in nTimerIndex
' since the Begin method was called. Both parameters are optional.
'
' nOutputType should be either 1 or 2, and you can use the constants
' outImmediateWindow and outMsgBox, repectively. This param
' determines where the output will go- either the immediate window or
' a message box. The description will be displayed along with the
' elpased time. If this param is omitted, the output goes to the immediate
' window.
'
' nTimerIndex is used to specify which timer you want to display the
' elapsed time for. (See the description in the Begin method, above).
' If omitted, timer number 0 (zero) is used.
'
'
'PROPERTIES:
'
'Elapsed(nTimerIndex)
' -Returns the number of seconds that have elapsed since the Begin
' method was called for the specified timer. If nTimerIndex is omitted,
' timer 0 (zero) is assumed.
'
'
Option Explicit
Public Enum OutputTypes
outImmediateWindow = 1
outMsgBox = 2
End Enum
Dim nBegin(10) As Single
Dim sDesc(10) As String
Public Sub Begin(Optional nTimerIndex As Integer, Optional sTimerDescription As String)

If (nTimerIndex < 0 Or nTimerIndex > 9) Then Exit Sub
If sTimerDescription = "" Then sTimerDescription = "Timer " & Trim(Str(nTimerIndex))

nBegin(nTimerIndex) = Timer
sDesc(nTimerIndex) = sTimerDescription

End Sub
Public Property Get Elapsed(Optional nTimerIndex As Integer) As Single

If (nTimerIndex < 0 Or nTimerIndex > 9) Then Exit Property
Elapsed = Val(Format(Timer - nBegin(nTimerIndex), "####.##"))

End Property

Public Sub ShowElapsed(Optional nOutputType As OutputTypes, Optional nTimerIndex As Integer)
If nOutputType = 0 Then nOutputType = outImmediateWindow

If nOutputType < outImmediateWindow Or nOutputType > outMsgBox Then Exit Sub

If nOutputType = outImmediateWindow Then
Debug.Print sDesc(nTimerIndex) & ": " & Elapsed(nTimerIndex) & " seconds"
Exit Sub
End If

If nOutputType = outMsgBox Then
MsgBox sDesc(nTimerIndex) & ": " & Elapsed(nTimerIndex) & " seconds", vbOKOnly, "Debug Timer"
Exit Sub
End If
End Sub

Rate DebugTimer (106(106 Vote))
DebugTimer.bas

DebugTimer Comments

No comments yet — be the first to post one!

Post a Comment

0/1000 characters