Click here to Skip to main content
Click here to Skip to main content

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.

    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

    Share

    About the Author

    Dean Wyant
    Web Developer
    United States United States
    No Biography provided

    Comments and Discussions

     
    GeneralRe: Nice class. This saved me a lot of time. PinmemberDean Wyant31-Oct-07 4:13 
    QuestionWhy not use critical sections? PinmemberBrian van der Beek24-Jun-03 6:27 
    AnswerRe: Why not use critical sections? PinmemberDean Wyant25-Jun-03 18:04 
    QuestionCPerfTimer under VC++? PinmemberDarkGuardian5-Jun-03 4:04 
    I work on a project under VC++ in german Edition.I wanted to include and use your timer to check out my performance.But it doesn't work.
    There is an error about it,so I can't compile it.Do I have to convert anything I the code or do you have an other idea,why it does not work?
    AnswerRe: CPerfTimer under VC++? PinmemberDean Wyant5-Jun-03 5:11 
    GeneralRe: CPerfTimer under VC++? PinmemberDarkGuardian5-Jun-03 20:24 
    GeneralRe: CPerfTimer under VC++? PinmemberDarkGuardian5-Jun-03 20:37 
    GeneralRe: CPerfTimer under VC++? Pinmemberbitkidoku26-Sep-06 16:05 
    Generalmissing stdafx.h Pinmembersteven76512-May-03 12:52 
    GeneralRe: missing stdafx.h PinmemberDean Wyant13-May-03 15:23 

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

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

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