VB icon

DebugTimer

Email
Submitted on: 1/1/2015 12:34:00 PM
By: Matthew Heydman (from psc cd)  
Level: Not Given
User Rating: By 105 Users
Compatibility: VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 1011
 
     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.
 

Windows API/Global Declarations:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
'Windows API/Global Declarations for :DebugTimer
'**************************************
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
		
		
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: DebugTimer
' Description: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.
' By: Matthew Heydman (from psc cd)
'**************************************

'Add a new Form to your project, and add 3 command buttons to the
'form (named Command1, Command2, and Command3). Then just
'paste the following code into the form:
Option Explicit
Dim i As Integer
Dim dbg As New clsDebugTimer
Private Sub Command1_Click()
 Me.MousePointer = vbHourglass
 
 'EXAMPLE 1 - VERY BASIC USAGE
 
 ' Start the timer
 dbg.Begin
 
 'Do something that will take a little time
 For i = 0 To 25000: DoEvents: Next
 
 'By default, calling the ShowElapsed method
 'will display the elapsed time in the immediate window
 dbg.ShowElapsed
 
 
 Me.MousePointer = vbDefault
 
End Sub
Private Sub Command2_Click()
 Me.MousePointer = vbHourglass
 'EXAMPLE 2 - USING THE PARAMETERS
 
 'Start the timer, this time passing a
 'timer index and a description
 dbg.Begin 0, "Loop from 0 to 25000"
 
 'Do something that takes time
 For i = 0 To 25000: DoEvents: Next
 
 'Display the elapsed time for timer index 0 in a message box
 dbg.ShowElapsed outMsgBox, 0
 
 
 Me.MousePointer = vbDefault
 
End Sub
Private Sub Command3_Click()
 Me.MousePointer = vbHourglass
 
 'EXAMPLE 3 - USING MULTIPLE TIMERS
 
 'Start the first timer- we'll use an index of 1
 'timer index and a description
 dbg.Begin 1, "Total Time"
 
'Start a second timer- (index 2)
'timer index and a description
dbg.Begin 2, "Count from 0 to 25000"
'Do something that takes time
For i = 0 To 25000: DoEvents: Next
'Display the elapsed time for the second timer
dbg.ShowElapsed outImmediateWindow, 2
 
 
'perform another loop like the one we just did above
dbg.Begin 2, "Count from 0 to 24999"
'Do something that takes time
For i = 0 To 24999: DoEvents: Next
'Display the elapsed time for the second timer
dbg.ShowElapsed outImmediateWindow, 2
 'Now display the elapsed time for the first timer
 dbg.ShowElapsed outImmediateWindow, 1
 
 
 Me.MousePointer = vbDefault
End Sub


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

1/20/2017 4:15:35 PMTinhorn

great prg but has lot of code won't work with VB4. if I fix for VB4 I will let you know

Enum

"Optional" must be >Variant <
Public Sub ..., Optional sTimerDescription As String)

Dim dbg As New clsDebugTimer


(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.