Click here to Skip to main content
13,804,400 members
Click here to Skip to main content
Add your own
alternative version


51 bookmarked
Posted 11 Apr 2000

The CPerfTimer Timer Class

, 27 Apr 2000
Rate this:
Please Sign up or sign in to vote.
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.


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.


About the Author

Dean Wyant
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionCPerfTimerT class ? Pin
Anonymous27-Dec-02 2:02
memberAnonymous27-Dec-02 2:02 
AnswerRe: CPerfTimerT class ? Pin
Dean Wyant28-Dec-02 3:40
memberDean Wyant28-Dec-02 3:40 
QuestionJava timer? Pin
sharonrz17-Dec-02 4:21
membersharonrz17-Dec-02 4:21 
QuestionLicense ? Pin
George13-Nov-02 21:30
memberGeorge13-Nov-02 21:30 
AnswerRe: License ? Pin
Dean Wyant16-Nov-02 5:48
memberDean Wyant16-Nov-02 5:48 
GeneralRe: License ? Pin
George16-Nov-02 6:51
memberGeorge16-Nov-02 6:51 
GeneralPerformance Issue Pin
AllanK6-Jan-03 9:19
memberAllanK6-Jan-03 9:19 
GeneralRe: Performance Issue Pin
Dean Wyant9-Jan-03 14:50
memberDean Wyant9-Jan-03 14:50 
A call every 2 seconds or so would not be a problem at all. Performance would only be a consideration if the calls were in a tight loop (100+ calls a second).
QuestionIs it a Timer or a TimeCounter? Pin
Ming Liu15-Sep-02 18:35
memberMing Liu15-Sep-02 18:35 
AnswerRe: Is it a Timer or a TimeCounter? Pin
Dean Wyant19-Sep-02 10:14
memberDean Wyant19-Sep-02 10:14 
GeneralLack of stdafx.h file. Pin
Kisa13-Aug-02 6:37
memberKisa13-Aug-02 6:37 
GeneralRe: Lack of stdafx.h file. Pin
Kisa14-Aug-02 1:33
memberKisa14-Aug-02 1:33 
GeneralRe: Lack of stdafx.h file. Pin
Dean Wyant16-Aug-02 5:25
memberDean Wyant16-Aug-02 5:25 
GeneralRe: Lack of stdafx.h file. Pin
5-Apr-04 14:41
suss5-Apr-04 14:41 
GeneralRe: Lack of stdafx.h file. Pin
Dean Wyant7-Apr-04 15:40
memberDean Wyant7-Apr-04 15:40 
QuestionError Message ?? Pin
18-Mar-02 8:58
suss18-Mar-02 8:58 
AnswerRe: Error Message ?? Pin
Dean Wyant21-Mar-02 5:34
memberDean Wyant21-Mar-02 5:34 
Generalexample please Pin
8-Dec-01 21:05
suss8-Dec-01 21:05 
GeneralRe: example please Pin
Dean Wyant9-Dec-01 10:38
memberDean Wyant9-Dec-01 10:38 
GeneralRe: example please Pin
10-Dec-01 17:06
suss10-Dec-01 17:06 
GeneralTimer runs to quick Pin
5-Mar-01 1:01
suss5-Mar-01 1:01 
GeneralRe: Timer runs to quick Pin
5-Mar-01 7:04
suss5-Mar-01 7:04 
GeneralUsage Notes Pin
27-Jan-01 12:15
suss27-Jan-01 12:15 
GeneralVery useful... Pin
Kabir5-Sep-00 1:37
memberKabir5-Sep-00 1:37 
GeneralThe poor rating was due to compile problem - update should be posted soon Pin
Dean Wyant14-Apr-00 23:44
memberDean Wyant14-Apr-00 23:44 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.181218.1 | Last Updated 28 Apr 2000
Article Copyright 2000 by Dean Wyant
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid