Click here to Skip to main content
Licence 
First Posted 11 Apr 2000
Views 168,022
Bookmarked 45 times

The CPerfTimer timer class

By | 27 Apr 2000 | Article
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

    About the Author

    Dean Wyant

    Web Developer

    United States United States

    Member



    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    GeneralQueryPerformanceCounter fails sometimes, when structmember alignment is set to 1 byte Pinmemberhund120004:42 20 Nov '08  
    GeneralRe: QueryPerformanceCounter fails sometimes, when structmember alignment is set to 1 byte PinmemberDean Wyant5:39 20 Nov '08  
    QuestionApplication Error When Calling Start(TRUE) Pinmembershizulooi23:08 30 Oct '07  
    AnswerRe: Application Error When Calling Start(TRUE) PinmemberDean Wyant3:04 31 Oct '07  
    Questionproblem compiling CPerfTimer on VC++ Express Pinmemberabarund13:08 27 Aug '06  
    AnswerRe: problem compiling CPerfTimer on VC++ Express PinmemberDean Wyant4:30 28 Aug '06  
    AnswerRe: problem compiling CPerfTimer on VC++ Express PinmemberCoderBruce8:01 15 Jul '10  
    GeneralPossible bug Pinmembermetimmee12:39 7 May '06  
    GeneralRe: Possible bug PinmemberDean Wyant5:08 12 May '06  
    GeneralLaptop Pinmemberzvivered7:11 30 Mar '05  
    GeneralRe: Laptop PinmemberDean Wyant12:21 30 Mar '05  
    GeneralWindows CE PinmemberGrimoire12:31 2 Nov '04  
    GeneralExcellent Pinmemberthought1:48 20 Jan '04  
    GeneralNice class. This saved me a lot of time. Pinmembervarnk10:06 19 Sep '03  
    GeneralRe: Nice class. This saved me a lot of time. PinmemberDean Wyant3:13 31 Oct '07  
    I know this reply is very late...
    The m_Adjust does get a value on slower processors. Perhaps your processor can run the code in less than a microsecond. m_Adjust adjusts for the time the code takes. This allows the timer to get close to microsecond accuracy. Without it, the accuracy would suffer on slower processors.
     
    The only time you might want to use CPerfTimerT is if you are using the same instance (variable) in multiple threads. Since you are not sharing an instance of CPerfTimer between threads, CPerfTimerT is not needed. I only included CPerfTimerT it because someone asked for a thread safe version. I have never had a need for it.

    QuestionWhy not use critical sections? PinmemberBrian van der Beek5:27 24 Jun '03  
    AnswerRe: Why not use critical sections? PinmemberDean Wyant17:04 25 Jun '03  
    QuestionCPerfTimer under VC++? PinmemberDarkGuardian3:04 5 Jun '03  
    AnswerRe: CPerfTimer under VC++? PinmemberDean Wyant4:11 5 Jun '03  
    GeneralRe: CPerfTimer under VC++? PinmemberDarkGuardian19:24 5 Jun '03  
    GeneralRe: CPerfTimer under VC++? PinmemberDarkGuardian19:37 5 Jun '03  
    GeneralRe: CPerfTimer under VC++? Pinmemberbitkidoku15:05 26 Sep '06  
    Generalmissing stdafx.h Pinmembersteven76511:52 12 May '03  
    GeneralRe: missing stdafx.h PinmemberDean Wyant14:23 13 May '03  
    Generalmissing stdafx.h Pinmembersteven76511:52 12 May '03  

    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.

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