Click here to Skip to main content
13,251,605 members (70,480 online)
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
  • Download source files - 3 Kb
  • 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

    GeneralQueryPerformanceCounter fails sometimes, when structmember alignment is set to 1 byte Pin
    hund1200020-Nov-08 5:42
    memberhund1200020-Nov-08 5:42 
    GeneralRe: QueryPerformanceCounter fails sometimes, when structmember alignment is set to 1 byte Pin
    Dean Wyant20-Nov-08 6:39
    memberDean Wyant20-Nov-08 6:39 
    QuestionApplication Error When Calling Start(TRUE) Pin
    shizulooi31-Oct-07 0:08
    membershizulooi31-Oct-07 0:08 
    AnswerRe: Application Error When Calling Start(TRUE) Pin
    Dean Wyant31-Oct-07 4:04
    memberDean Wyant31-Oct-07 4:04 
    Questionproblem compiling CPerfTimer on VC++ Express Pin
    abarund27-Aug-06 14:08
    memberabarund27-Aug-06 14:08 
    AnswerRe: problem compiling CPerfTimer on VC++ Express Pin
    Dean Wyant28-Aug-06 5:30
    memberDean Wyant28-Aug-06 5:30 
    AnswerRe: problem compiling CPerfTimer on VC++ Express Pin
    CoderBruce15-Jul-10 9:01
    memberCoderBruce15-Jul-10 9:01 
    GeneralPossible bug Pin
    metimmee7-May-06 13:39
    membermetimmee7-May-06 13:39 
    GeneralRe: Possible bug Pin
    Dean Wyant12-May-06 6:08
    memberDean Wyant12-May-06 6:08 
    GeneralLaptop Pin
    zvivered30-Mar-05 8:11
    memberzvivered30-Mar-05 8:11 
    GeneralRe: Laptop Pin
    Dean Wyant30-Mar-05 13:21
    memberDean Wyant30-Mar-05 13:21 
    GeneralWindows CE Pin
    Grimoire2-Nov-04 13:31
    memberGrimoire2-Nov-04 13:31 
    GeneralExcellent Pin
    thought20-Jan-04 2:48
    memberthought20-Jan-04 2:48 
    GeneralNice class. This saved me a lot of time. Pin
    varnk19-Sep-03 11:06
    membervarnk19-Sep-03 11:06 
    GeneralRe: Nice class. This saved me a lot of time. Pin
    Dean Wyant31-Oct-07 4:13
    memberDean Wyant31-Oct-07 4:13 
    QuestionWhy not use critical sections? Pin
    Brian van der Beek24-Jun-03 6:27
    memberBrian van der Beek24-Jun-03 6:27 
    AnswerRe: Why not use critical sections? Pin
    Dean Wyant25-Jun-03 18:04
    memberDean Wyant25-Jun-03 18:04 
    That would be fine. It is easy to change to use a critical section.
    However, the performance counter functions themselves are fairly slow. If performance is an issue, it would probably be a good idea to use some other method of timing. I wrote this class to simply wrap the functions in a class that could provide accurate timing, mimimal memory use, and ease of use. At the time, I did not think to use a critical section because I thought a mutex was fast enough. There is info on the web about high performance methods of timing that would probably be a better choice for programs that will call into the timer code a lot in a short period of time. IOW, programs that need the timer calls to be very fast. This classes timer calls themselves are not very fast. Wink | ;)

    QuestionCPerfTimer under VC++? Pin
    DarkGuardian5-Jun-03 4:04
    memberDarkGuardian5-Jun-03 4:04 
    AnswerRe: CPerfTimer under VC++? Pin
    Dean Wyant5-Jun-03 5:11
    memberDean Wyant5-Jun-03 5:11 
    GeneralRe: CPerfTimer under VC++? Pin
    DarkGuardian5-Jun-03 20:24
    memberDarkGuardian5-Jun-03 20:24 
    GeneralRe: CPerfTimer under VC++? Pin
    DarkGuardian5-Jun-03 20:37
    memberDarkGuardian5-Jun-03 20:37 
    GeneralRe: CPerfTimer under VC++? Pin
    bitkidoku26-Sep-06 16:05
    memberbitkidoku26-Sep-06 16:05 
    Generalmissing stdafx.h Pin
    steven76512-May-03 12:52
    membersteven76512-May-03 12:52 
    GeneralRe: missing stdafx.h Pin
    Dean Wyant13-May-03 15:23
    memberDean Wyant13-May-03 15:23 
    Generalmissing stdafx.h Pin
    steven76512-May-03 12:52
    membersteven76512-May-03 12: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.

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