Gustav Brock
Gustav at cactus.dk
Tue May 9 09:24:05 CDT 2006
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