Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 22nd, 2004, 01:52 PM
Authorized User
 
Join Date: Jun 2004
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Debugging Timer

Hi:


Could anyone recommend a good way to show the speed (in minutes/seconds) of a sub/function to run?

I am looking for something like this. Function Losses() takes 1:06 to return a value.


Thank you,
Adrian T
 
Old June 23rd, 2004, 03:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Put this code inside a new Class module & call the module something like Stopwatch:

' **********************************************
' Start Code
' **********************************************

Option Explicit

Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private mlngStart As Long

Public Sub StartTimer()
' Store away the starting time.

    mlngStart = timeGetTime

End Sub

Property Get IsRunning() As Boolean
' Return True if the stopwatch is running, False otherwise.

    IsRunning = (mlngStart <> 0)

End Property

Property Get ElapsedTime() As Long
' Return the elapsed time.

    If IsRunning Then
        ElapsedTime = (timeGetTime - mlngStart)
    Else
        ElapsedTime = 0
    End If

End Property

Property Get TimerStarted() As Long
    ' Return the starting time.
    TimerStarted = mlngStart
End Property

Public Function StopTimer() As Long
' Retrieve the elapsed time, and reset the start time to 0 (so IsRunning will think you're not currently running).

    If IsRunning Then
        StopTimer = (timeGetTime - mlngStart)
    Else
        StopTimer = 0
    End If

    ' Reset the timer.
    mlngStart = 0

End Function

' **********************************************
' End Code
' **********************************************

Now in any other sub / function in any other module (in the same project) you can use our new Stopwatch with the following code:

' **********************************************
' Start Code
' **********************************************

Dim my Timer as Stopwatch ' NB must be same as Class module name

    ' ... Other code

    Set myTimer = New Stopwatch ' Intialise object

    ' ... Other Code

    myTimer.StartTimer

    ' ... Main Body of Function / Sub

    MsgBox "Function / Sub took " & myTimer.ElapsedTime & _
    " milliseconds to run"

    ' ... Other Code

' **********************************************
' End Code
' **********************************************

HTH,
James
 
Old June 23rd, 2004, 08:54 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

above code is fine, depending on how accurate you want it

check out MSKB article 172338 for timing your functions.

http://support.microsoft.com/default...b;EN-US;172338

Long and short of it is that you can use QueryPerformanceCounter in combination with QueryPerformanceFrequency to get a truer timing of your code





Similar Threads
Thread Thread Starter Forum Replies Last Post
Countdown timer DennyLoi Javascript How-To 2 November 27th, 2007 08:01 AM
Timer Dhanapal ADO.NET 2 April 16th, 2007 04:58 AM
Timer prasanta2expert Access VBA 1 November 8th, 2006 09:28 AM
timer amerei ASP.NET 1.0 and 1.1 Professional 0 October 15th, 2005 10:49 PM
Timer Control sankar_massoft General .NET 0 December 21st, 2004 11:53 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.