time count (1 Viewer)

smig

Registered User.
Local time
Tomorrow, 01:27
Joined
Nov 25, 2009
Messages
2,209
Hello,

I want to test how long (in MiliSeconds) does it take to run a routine.
The Time() function will only give me 1 second intervals.
How do i do that ?

Thank you,
Tal
 

ccondran08

Registered User.
Local time
Tomorrow, 07:27
Joined
Feb 27, 2014
Messages
58
You could try this ;

Dim time1 As Double, time2 As Double

time1 = Timer

<your routine here>

time2 = Timer

MsgBox Format (time2 - time1, "0:000\s\ec")
 

jdraw

Super Moderator
Staff member
Local time
Today, 19:27
Joined
Jan 23, 2006
Messages
15,364
Here's a small routine that should do the job. It involves a class module, called clsTimer, and a test routine TestTimer showing how to set up and use clsTimer.

The class module goes in a separate module. The code is:
Code:
Option Compare Database
Option Explicit
'
'This class never loads children, never does anything that should cause problems
'
Private Declare Function apiGetTime Lib "winmm.dll" _
                                    Alias "timeGetTime" () As Long

Private lngStartTime As Long

Private Sub Class_Initialize()
10        StartTimer
End Sub

'THESE FUNCTIONS / SUBS ARE USED TO IMPLEMENT CLASS FUNCTIONALITY '*+Class function / sub declaration
Function EndTimer()
'calculate duration by getting current time and subtracting start time
10        EndTimer = apiGetTime() - lngStartTime
End Function

Sub StartTimer()
'get the time when the Timer starts
10        lngStartTime = apiGetTime()
End Sub

The test routine is
Code:
'---------------------------------------------------------------------------------------
' Procedure : testTimer
' Author    : Jack (from access group discussions)
' Created   : 11/28/2010
' Purpose   : To show the use of clsTimer

'With a class you can instantiate 1 or a thousand of these timers and
'EACH INSTANCE has its own timer storage variable safely encapsulated in its own class header.
'
'Dim lclsTimer0 As clsTimer
'Dim lclsTimer1 As clsTimer
'Dim lclsTimer2 As clsTimer
'.
'.
'.
'Dim lclsTimer999 As clsTimer

'There you go, 1,000 DIFFERENT timers all happily timing their own pieces
'of your code, measuring various forms opening, reports opening,
'while loops whiling.
'
'The tick count is in fact 1 millisecond, and USUALLY you will get results to 1 millisecond.
'
'Whatever the count, it is certainly relative and can show relative times.
'
'More time stuff at   http://www.devx.com/dbzone/Article/39046
'
'---------------------------------------------------------------------------------------
' Last Modified:
'
' Inputs: N/A
' Dependency: N/A
'------------------------------------------------------------------------------
'
Sub testTimer()

    On Error GoTo testTimer_Error

    Set lclsTimer0 = New clsTimer
    Set lclsTimer1 = New clsTimer
    Set lclsTimer2 = New clsTimer

    Dim myVar As Long
    While myVar < 100000000
        If myVar = 75000000 Then
            Debug.Print "Timer0 " & lclsTimer0.EndTimer & " MyVar reached " & myVar
        End If
        If myVar = 10000000 Then
            Debug.Print "Timer1 " & lclsTimer1.EndTimer & " MyVar reached " & myVar
        End If
        myVar = myVar + 1
    Wend
    Set lclsTimer3 = New clsTimer                                                               'start Timer3
    Debug.Print "Timer2 " & lclsTimer2.EndTimer & " MyVar reached " & myVar
    Debug.Print "Timer3   Processing this last debug took " & lclsTimer3.EndTimer & "  tick(s)"    'Stop Timer3
    On Error GoTo 0
    Exit Sub

testTimer_Error:

    MsgBox "Error " & Err.number & " (" & Err.Description & ") in procedure testTimer of Module Module1"
End Sub

My test output on a Lenovo z710 Access 2010
Code:
Timer1 231 MyVar reached 10000000
Timer0 1712 MyVar reached 75000000
Timer2 2285 MyVar reached 100000000
Timer3   Processing this last debug took 1  ticks

Good luck.
 

expat1000

Registered User.
Local time
Today, 16:27
Joined
Apr 6, 2012
Messages
18
Old post and code but I just want to say it works perfectly for me. Way better than a form timer.
 

isladogs

MVP / VIP
Local time
Today, 23:27
Joined
Jan 14, 2017
Messages
18,186
As you say, very old thread.
However, if anyone is interested, I have several timer tests that will work to millisecond intervals and in one case microseconds.
Note that higher resolution does not necessarily indicate greater accuracy.
See Timer Comparison Tests
 

Users who are viewing this thread

Top Bottom