Important alert: (current site time 7/16/2013 4:16:30 AM EDT)
 

VB icon

DebugTimer

Email
Submitted on: 12/8/1997
By: Matthew Heydman  
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: 26978
 
     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!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' 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
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=754&lngWId=1'for details.'**************************************

'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

4/16/1999 7:08:00 AMMatt Dawdy

This is INCREDIBLY useful. Thank you for posting it. I am going to make this into a dll and then be able to reference it from any project, and that way I can include in during development, and then dump it quickly for final production.
(If this comment was disrespectful, please report it.)

 
5/2/1999 6:22:00 PMLloyd Cox

Will this work in VBA? Specifically Word 97. I have a few templates that get bogged down and just seem to sit there at times and have been looking for a way to find out where the problem is.

I tried to paste the code into VBA, but got some errors?

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

 
4/4/2001 3:24:40 PMVijay

This is kewl. Even I had done this but it wasnt that robust. Good work keep it up.

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

 
10/13/2002 1:44:36 PMMatthew Heydman

Hmmm, some of the code is not appearing above... it used to, but I dunno what happened. You can view the complete post by clicking the "copy and paste friendly" link above above the code.
(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.