[AccessD] Timer class suggestions...

Heenan, Lambert Lambert.Heenan at AIG.com
Tue May 9 09:52:46 CDT 2006


To answer my own question...

I added the following block of code to the article's test code...

      '
      ' Time timer
      '
        Dim sCount1 As Single, sCount2 As Single

        Debug.Print
        Loops = 0
        sCount1 = Timer()
        Do
          sCount2 = Timer()
          Loops = Loops + 1
        Loop Until sCount1 <> sCount2
        Debug.Print "timer minimum resolution: "; _
                    (sCount2 - sCount1); "ms"
        Debug.Print "Took"; Loops; "loops"

... And here is the result...

Test_Timers
Start Value: 2196010.9816
End Value: 2196010.9826
QueryPerformanceCounter minimum resolution: 1/3579545 sec
API Overhead:  2.79365114840015E-06 seconds

GetTickCount minimum resolution:  10 ms
Took 52952 loops

timeGetTime minimum resolution:  1 ms
Took 4795 loops

timer minimum resolution:  0.0078125 ms
Took 11716 loops

So Timer has about the same resolution as timeGetTime, but it runs about
twice as fast.

Lambert

-----Original Message-----
From: Heenan, Lambert 
Sent: Tuesday, May 09, 2006 10:43 AM
To: 'Access Developers discussion and problem solving'
Subject: RE: [AccessD] Timer class suggestions...


Hello Gustav,

As the kBase article says that GetTickCount  and TimeGetTime have ~10ms
resolution I wonder if there is anything to be gained in using them rather
that the built-in function Timer, which also has ~10ms resolution?

Lambert

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Tuesday, May 09, 2006 10:24 AM
To: accessd at databaseadvisors.com
Subject: Re: [AccessD] Timer class suggestions...


Hi Bobby

Never heard of those QueryPerformance-things so I located this page:

  http://support.microsoft.com/kb/q172338

However, the info here is wrong. 
timeGetTime is certainly accurate to the millisecond, thus the most precise
of all standard timer functions. But you must specify to use a resolution of
1 ms. 

1. Insert this declaration:

Private Declare Function timeBeginPeriod Lib "winmm.dll" ( _
  ByVal uPeriod As Long) _
  As Long

2. Modify the code like this:

      '
      ' Time GetTickCount
      '
      ' Set resolution of timer to 1 ms.
        timeBeginPeriod 1
        Debug.Print
        Loops = 0
        Count1 = GetTickCount()

3. Run code:

Start Value: 9221238,3029
End Value: 9221238,3039
QueryPerformanceCounter minimum resolution: 1/3579545 sec
API Overhead:  2,79365114840015E-06 seconds

GetTickCount minimum resolution:  15 ms
Took 23289 loops

timeGetTime minimum resolution:  1 ms
Took 187 loops

As for your class I've never had extensive timer needs so I have no
comments.

/gustav

>>> bheid at appdevgrp.com 09-05-2006 13:37:51 >>>
Hey,

I had a need to time some code the other day and decided to come up with a
somewhat generic class to help perform this task.  I'd like to ask for your
opinion/suggestions about what I came up with.

This is what I came up with.

First, since all users are on Win XP, I am using QueryPerformanceCounter and
QueryPerformanceFrequency to calculate timings.

There are three classes: clsTimer, clsTimerData, and clsTimerInstance

clsTimer is the main class.  It contains 2 collections that hold instances
of the other two classes and a timestamp of the time that the class was
instantiated (called RunTS).

clsTimerInstance holds data about a timer that has been started (ID, start
value of timer, and a time stamp).

clsTimerData holds the complete timer data record for a given timer event
(ID, start value of timer, end value of timer, difference between end and
start, elapsed time in seconds, time stamp of timer, and RunTS).  This class
holds the completed timer data until the data is persisted.

To use the timers, an instance of clsTimer is instantiated.

For each timer that is to be started, make a call to the clsTimer.StartTimer
with an ID (long value) that  is used as the key for the timer instance.
This function returns an index to that timer that is used to stop the timer.
This function creates an instance of the clsTimerInstance class that
contains the starting values of the timer and adds it to the Instance
collection.

When a given timer is stopped, the ending timer value is obtained.  The
starting values are located in the Instance collection.  A new clsTimerData
instance is created and populated with the timer data and added to the
TimerData collection.  The clsTimerInstance collection item is then deleted.

The data can be persisted to a table with a call to clsTimer.PersistData.
Right now, this function simply iterates the TimerData collection and dumps
it to a table.  I plan on being able to pass this function a value so that
this function can also call two other functions to dump to a formatted flat
file and a CSV file.


To use the timers is a simple as:

Dim cTimer as new clsTimer

...

	x=ctimer.StartTimer(1)
	'some code
	cTimer.StopTimer 1

	x=ctimer.StartTimer(1)
	for i=0 to 100000
		y=cTimer.StartTimer(2)	'we can nest them
		'some more code
		cTimer.StopTimer 2
		next i

	ctimer.StopTimer 1
	ctimer.PersistData

	set ctimer=nothing

The output would look something like (from a different test run):

TimerID StartVal		EndVal	    Elapsed	Seconds TimeStamp
RunTS
1	  215002386.2371	215002420.5905  34.3534	0.096	  5/9/2006
7:32:13 AM  5/9/2006 7:32:13 AM
2 ...
2 ...
2 ...

How does this sound to you all?  Thanks for even making it this far!

Thanks,
Bobby

-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com



More information about the AccessD mailing list