Click here to Skip to main content
15,885,366 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 242.1K   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

 
GeneralRe: Lack of stdafx.h file. Pin
Kisa14-Aug-02 0:33
Kisa14-Aug-02 0:33 
GeneralRe: Lack of stdafx.h file. Pin
Dean Wyant16-Aug-02 4:25
Dean Wyant16-Aug-02 4:25 
GeneralRe: Lack of stdafx.h file. Pin
5-Apr-04 13:41
suss5-Apr-04 13:41 
GeneralRe: Lack of stdafx.h file. Pin
Dean Wyant7-Apr-04 14:40
Dean Wyant7-Apr-04 14:40 
QuestionError Message ?? Pin
18-Mar-02 7:58
suss18-Mar-02 7:58 
AnswerRe: Error Message ?? Pin
Dean Wyant21-Mar-02 4:34
Dean Wyant21-Mar-02 4:34 
Generalexample please Pin
8-Dec-01 20:05
suss8-Dec-01 20:05 
GeneralRe: example please Pin
Dean Wyant9-Dec-01 9:38
Dean Wyant9-Dec-01 9:38 
An example would be different according to whether you use MFC or not. Also, what kind of progam (dialog, console, etc.) would dictate how tp show output.

Using MFC:

Add CPerfTimer to your project.

#include "PerfTimer.h"

CString S;
CPerfTimer T;

T.Start(TRUE);
//.... do something
S.Format("%6.6f",T.Elapsed());
// Timer is still running, to stop it use T.Stop();
// Show S according to the type of program


Using system library:

#include "PerfTimer.h"

char S[20];
CPerfTimer T;

T.Start(TRUE);
//.... do something
sprintf(S,"%6.6f",T.Elapsed());
// Timer is still running, to stop it use T.Stop();
// Show S according to the type of program.

Note that the time will seem unstable or inaccurate if running
in debug mode. It is not wrong, the debugger overhead can cause iconsistencies. Also remember that multitasking operating systems
may cause other threads to run in the middle of a timimg operation which can cause times to be inconsistent. However, all times the CPerfTimer class reports are the correct real time (within the resolution and accounting for overhead (about +-0.2us). Also, CPerfTimer is not meant for timimg long operations because the QueryPerformanceFrequency call is not accurate enough to maintain resolution over many hours or days.


GeneralRe: example please Pin
10-Dec-01 16:06
suss10-Dec-01 16:06 
GeneralTimer runs to quick Pin
5-Mar-01 0:01
suss5-Mar-01 0:01 
GeneralRe: Timer runs to quick Pin
5-Mar-01 6:04
suss5-Mar-01 6:04 
GeneralUsage Notes Pin
27-Jan-01 11:15
suss27-Jan-01 11:15 
GeneralVery useful... Pin
Kabir5-Sep-00 0:37
Kabir5-Sep-00 0:37 
GeneralThe poor rating was due to compile problem - update should be posted soon Pin
Dean Wyant14-Apr-00 22:44
Dean Wyant14-Apr-00 22:44 
GeneralUpdated posted Pin
Chris Maunder14-Apr-00 22:50
cofounderChris Maunder14-Apr-00 22:50 

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.