Timing code (1 Viewer)

Peter Bellamy

Registered User.
Local time
Today, 15:12
Joined
Dec 3, 2005
Messages
295
This is probaly known to the gurus on this forum but it came as news to me when I was searching to a solution for another problem.

There is a timer available in Access which you can use, for example, to time the speed of your code, which can be as accurate a 1ms.
It can be declared where you want it, I put it an a module to make available everywhere.

Code:
Declare Function timeGetTime Lib "WINMM" () As Long

A Google for 'timeGetTime' will give you the details

It is a timer that starts when Windows starts and does not rollover for about 50 days. So subtracting your first call from subsequent ones gives you the intervals.

Hope you find it interesting!

pnb
 

MarkK

bit cruncher
Local time
Today, 07:12
Joined
Mar 17, 2004
Messages
8,187
VBA also has a timer that returns the number of seconds since midnight without making an API call. To time code you can do ...
Code:
Sub TestTimer()
   Dim t As Single
   t = Timer
   TakesTime
   t = Timer - t
   MsgBox Format(t, "0.000")
End Sub

Sub TakesTime()
   Dim i As Integer
   For i = 0 To 100
      Debug.Print i
   Next
End Sub
 

Peter Bellamy

Registered User.
Local time
Today, 15:12
Joined
Dec 3, 2005
Messages
295
True but it is not as accurate !
I believe the resolution is only one second
 

ChrisO

Registered User.
Local time
Tomorrow, 00:12
Joined
Apr 30, 2003
Messages
3,202
From the point of view of interesting he’s another one.

Access has what is called, by any other name, context sensitive help. This means we can place the cursor on a word and press the F1 key. I think I can recall some programs from the mid eighties having this facility. It actually surprises me that some programmers are not aware of it. I remember a post where someone said they had been using Access for about three years and didn’t know about it; they then thought it was the best thing since sliced bread.

But what it boils down to is we can make fast assessments of a word without resorting to asking a question or making a statement. So, in this case, we could place the cursor on the word Timer and press the F1 key. The help file simply states that, unless we are using a MAC, the timer function also returns the fractional part of a second. They don’t say what the resolution of the fractional part actually is but they do say it’s fractional.

More interesting things about timekeeping: -

http://blogs.msdn.com/b/larryosterman/archive/2005/09/08/462477.aspx

Hope that helps.
Chris.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 15:12
Joined
Sep 12, 2006
Messages
15,710
thats a good point Chris,

I use Timer, and without thinking I have got into the habit of doing

dim time as long
time=timer


and then thinking timer returns whole seconds only


Instead if you just do

dim time as double
time=timer


then it returns the fractional part. and I expect it calls some API anyway.
 

MarkK

bit cruncher
Local time
Today, 07:12
Joined
Mar 17, 2004
Messages
8,187
...cursor on the word 'timer' ... hitting F1 ... almost there ...
Timer Function

Returns a Single representing the number of seconds elapsed since midnight.

Syntax

Timer

Remarks

In Microsoft Windows the Timer function returns fractional portions of a second. On the Macintosh, timer resolution is one second.

Example
This example uses the Timer function to pause the application. The example also uses DoEvents to yield to other processes during the pause.

Code:
Dim PauseTime, Start, Finish, TotalTime
If (MsgBox("Press Yes to pause for 5 seconds", 4)) = vbYes Then
    PauseTime = 5    ' Set duration.
    Start = Timer    ' Set start time.
    Do While Timer < Start + PauseTime
        DoEvents    ' Yield to other processes.
    Loop
    Finish = Timer    ' Set end time.
    TotalTime = Finish - Start    ' Calculate total time.
    MsgBox "Paused for " & TotalTime & " seconds"
Else
    End
End If
 

Users who are viewing this thread

Top Bottom