Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / Objective C

The CPerfTimer Timer Class

Rate me:
Please Sign up or sign in to vote.
4.21/5 (23 votes)
27 Apr 2000 241.8K   3.2K   53   52
This class encapsulates QueryPerformanceCounter for high precision timing.

Updated: The class is now thread safe.

High resolution timing is supported in Win32 by the QueryPerformanceCounter and QueryPerformanceFrequency API calls. The timer resolution varies with the processor. Today's high speed processors have a timer resolution of less than a microsecond. Of course, this is a much better resolution than the GetTickCount API!

Using the QueryPerformanceCounter calls directly takes too much typing and the resulting code is usually hard to read. So, I looked all over the net for a timer class but I could not find one that suited me. I wrote a simple and powerful timer class and named it CPerfTimer. I wrote this a long time ago but I still have not had the need to update it. Searching the net, I could not find a timer class as simple to use and as useful as CPerfTimer. Several other timer classes are available and they vary as to their simplicity and usefulness. I do not claim that this is the end-all-be-all of timer classes; but, I hope that someone will find it useful enough to refrain from taking the time to write yet another timer class.

This class is simple to use. Just declare a variable as type CPerfTimer, call Start() to start timing and call Stop() to stop timing. You can pause a timer by calling Stop() and then you can call Start() to resume. Retrieve the elapsed time by calling an Elapsed...() function. Assignment, addition, subtraction and comparison are supported. There are a few information calls available also. All calls except Start and Stop can be performed on a timer without stopping it.

I have not included separate documentation or example application. The code is fairly well documented and the above paragraph was copied from the CPerfTimer.h file. Methods for adding, subtracting and comparing CPerfTimers and seconds (double) are also included. And, a CPerfTimerT class that should be thread safe (no extensive testing has been done). Please inform me if any bugs are found or you have an idea for an enhancement.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralQueryPerformanceCounter fails sometimes, when structmember alignment is set to 1 byte Pin
hund1200020-Nov-08 4:42
hund1200020-Nov-08 4:42 
GeneralRe: QueryPerformanceCounter fails sometimes, when structmember alignment is set to 1 byte Pin
Dean Wyant20-Nov-08 5:39
Dean Wyant20-Nov-08 5:39 
QuestionApplication Error When Calling Start(TRUE) Pin
shizulooi30-Oct-07 23:08
shizulooi30-Oct-07 23:08 
AnswerRe: Application Error When Calling Start(TRUE) Pin
Dean Wyant31-Oct-07 3:04
Dean Wyant31-Oct-07 3:04 
Questionproblem compiling CPerfTimer on VC++ Express Pin
abarund27-Aug-06 13:08
abarund27-Aug-06 13:08 
AnswerRe: problem compiling CPerfTimer on VC++ Express Pin
Dean Wyant28-Aug-06 4:30
Dean Wyant28-Aug-06 4:30 
AnswerRe: problem compiling CPerfTimer on VC++ Express Pin
CoderBruce15-Jul-10 8:01
CoderBruce15-Jul-10 8:01 
GeneralPossible bug Pin
metimmee7-May-06 12:39
metimmee7-May-06 12:39 
GeneralRe: Possible bug Pin
Dean Wyant12-May-06 5:08
Dean Wyant12-May-06 5:08 
GeneralLaptop Pin
zvivered30-Mar-05 7:11
zvivered30-Mar-05 7:11 
I'm using CPerfTimer for a long time. It works great.
Few days ago I tried it on a laptop with Windows XP.
I wrote:

Timer.Start (TRUE);<br />
while (1)<br />
{<br />
     Timetag=Timer.Elapsed ()<br />
     Sleep (40)<br />
}


The value of Timetag was set to 0 every few seconds and then grew up.
The same code worked great on a PC with Windows 2000.
The value of Timetag was not set to 0.

Can you help ?
GeneralRe: Laptop Pin
Dean Wyant30-Mar-05 12:21
Dean Wyant30-Mar-05 12:21 
GeneralWindows CE Pin
Grimoire2-Nov-04 12:31
Grimoire2-Nov-04 12:31 
GeneralExcellent Pin
[James Pullicino]20-Jan-04 1:48
[James Pullicino]20-Jan-04 1:48 
GeneralNice class. This saved me a lot of time. Pin
varnk19-Sep-03 10:06
varnk19-Sep-03 10:06 
GeneralRe: Nice class. This saved me a lot of time. Pin
Dean Wyant31-Oct-07 3:13
Dean Wyant31-Oct-07 3:13 
QuestionWhy not use critical sections? Pin
Brian van der Beek24-Jun-03 5:27
Brian van der Beek24-Jun-03 5:27 
AnswerRe: Why not use critical sections? Pin
Dean Wyant25-Jun-03 17:04
Dean Wyant25-Jun-03 17:04 
QuestionCPerfTimer under VC++? Pin
DarkGuardian5-Jun-03 3:04
DarkGuardian5-Jun-03 3:04 
AnswerRe: CPerfTimer under VC++? Pin
Dean Wyant5-Jun-03 4:11
Dean Wyant5-Jun-03 4:11 
GeneralRe: CPerfTimer under VC++? Pin
DarkGuardian5-Jun-03 19:24
DarkGuardian5-Jun-03 19:24 
GeneralRe: CPerfTimer under VC++? Pin
DarkGuardian5-Jun-03 19:37
DarkGuardian5-Jun-03 19:37 
GeneralRe: CPerfTimer under VC++? Pin
bitkidoku26-Sep-06 15:05
bitkidoku26-Sep-06 15:05 
Generalmissing stdafx.h Pin
steven76512-May-03 11:52
steven76512-May-03 11:52 
GeneralRe: missing stdafx.h Pin
Dean Wyant13-May-03 14:23
Dean Wyant13-May-03 14:23 
Generalmissing stdafx.h Pin
steven76512-May-03 11:52
steven76512-May-03 11:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.