Click here to Skip to main content
Click here to Skip to main content
Go to top

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

     
    GeneralQueryPerformanceCounter fails sometimes, when structmember alignment is set to 1 byte Pinmemberhund1200020-Nov-08 4:42 
    GeneralRe: QueryPerformanceCounter fails sometimes, when structmember alignment is set to 1 byte PinmemberDean Wyant20-Nov-08 5:39 
    QuestionApplication Error When Calling Start(TRUE) Pinmembershizulooi30-Oct-07 23:08 
    AnswerRe: Application Error When Calling Start(TRUE) PinmemberDean Wyant31-Oct-07 3:04 
    Questionproblem compiling CPerfTimer on VC++ Express Pinmemberabarund27-Aug-06 13:08 
    AnswerRe: problem compiling CPerfTimer on VC++ Express PinmemberDean Wyant28-Aug-06 4:30 
    AnswerRe: problem compiling CPerfTimer on VC++ Express PinmemberCoderBruce15-Jul-10 8:01 
    GeneralPossible bug Pinmembermetimmee7-May-06 12:39 
    GeneralRe: Possible bug PinmemberDean Wyant12-May-06 5:08 
    GeneralLaptop Pinmemberzvivered30-Mar-05 7:11 
    GeneralRe: Laptop PinmemberDean Wyant30-Mar-05 12:21 
    GeneralWindows CE PinmemberGrimoire2-Nov-04 12:31 
    GeneralExcellent Pinmemberthought20-Jan-04 1:48 
    GeneralNice class. This saved me a lot of time. Pinmembervarnk19-Sep-03 10:06 
    GeneralRe: Nice class. This saved me a lot of time. PinmemberDean Wyant31-Oct-07 3:13 
    QuestionWhy not use critical sections? PinmemberBrian van der Beek24-Jun-03 5:27 
    AnswerRe: Why not use critical sections? PinmemberDean Wyant25-Jun-03 17:04 
    QuestionCPerfTimer under VC++? PinmemberDarkGuardian5-Jun-03 3:04 
    AnswerRe: CPerfTimer under VC++? PinmemberDean Wyant5-Jun-03 4:11 
    GeneralRe: CPerfTimer under VC++? PinmemberDarkGuardian5-Jun-03 19:24 
    GeneralRe: CPerfTimer under VC++? PinmemberDarkGuardian5-Jun-03 19:37 
    GeneralRe: CPerfTimer under VC++? Pinmemberbitkidoku26-Sep-06 15:05 
    Generalmissing stdafx.h Pinmembersteven76512-May-03 11:52 
    GeneralRe: missing stdafx.h PinmemberDean Wyant13-May-03 14:23 
    Generalmissing stdafx.h Pinmembersteven76512-May-03 11:52 
    GeneralOn multiprocessor machine Pinmemberwarmcold4-May-03 12:33 
    QuestionCPerfTimerT class ? PinsussAnonymous27-Dec-02 1:02 
    AnswerRe: CPerfTimerT class ? PinmemberDean Wyant28-Dec-02 2:40 
    QuestionJava timer? Pinsusssharonr117-Dec-02 3:21 
    QuestionLicense ? PinmemberGeorge13-Nov-02 20:30 
    AnswerRe: License ? PinmemberDean Wyant16-Nov-02 4:48 
    GeneralRe: License ? PinmemberGeorge16-Nov-02 5:51 
    GeneralPerformance Issue PinmemberAllanK6-Jan-03 8:19 
    GeneralRe: Performance Issue PinmemberDean Wyant9-Jan-03 13:50 
    QuestionIs it a Timer or a TimeCounter? Pinsussmiliu15-Sep-02 17:35 
    AnswerRe: Is it a Timer or a TimeCounter? PinmemberDean Wyant19-Sep-02 9:14 
    GeneralLack of stdafx.h file. PinmemberKisa13-Aug-02 5:37 
    GeneralRe: Lack of stdafx.h file. PinmemberKisa14-Aug-02 0:33 
    GeneralRe: Lack of stdafx.h file. PinmemberDean Wyant16-Aug-02 4:25 
    GeneralRe: Lack of stdafx.h file. Pinmemberpriyanair5-Apr-04 13:41 
    GeneralRe: Lack of stdafx.h file. PinmemberDean Wyant7-Apr-04 14:40 
    QuestionError Message ?? PinmemberAnonymous18-Mar-02 7:58 
    AnswerRe: Error Message ?? PinmemberDean Wyant21-Mar-02 4:34 
    Generalexample please PinmemberRamy8-Dec-01 20:05 
    GeneralRe: example please PinmemberDean Wyant9-Dec-01 9:38 
    GeneralRe: example please PinmemberRamy10-Dec-01 16:06 
    GeneralTimer runs to quick PinmemberRob Ottenhoff5-Mar-01 0:01 
    GeneralRe: Timer runs to quick PinmemberAnonymous5-Mar-01 6:04 
    GeneralUsage Notes PinmemberDean27-Jan-01 11:15 
    Several people have contacted me stating that they get inconsistent results. Results can be inconsistent for several reasons.
     
    1. Program is compiled for debugging. The program itself does not run consistently due to debug code and task switching. Only expect consistent results in release compiled code. In debug code, the first run of the timer may seem excessive due to the operation of the debug code. Subsequent runs may be more consistent. Of course, the times are correct, it is the operation of the program that is inconsistent.
     
    2. Task switching. Your code that is being timed can be interupted for a task switch. The time will reflect the true time which includes the time the other thread runs. This is unavoidable in a multi-threaded operating system. Some control may be gained by placing Sleep(0) in your code right before starting the timer. Sleep(0) will reset your threads time slice and make it more likely that your timed operation will not be interupted. Of course, if your operation takes more than one time slice, it will still get interupted. The priority of the thread affects task switching also. These are issues we have to deal with when running on a multi-thread OS. We have to remember that we are sharing the processor and the OS controls how much time we get. If anyone knows any other Win32 calls that may help control task switching, please let me know or post them here.
     
    3. Very short operations (< 1us?) cannot be timed accurately due to CPerfTimers overhead and resolution. Any operation that is shorter than the time it takes to Start and Stop the timer cannot be timed accurately. For instance, if you put the timer inside a tight loop, you may be trying to time an operation that is less than the timers resolution. The result will be 0.
     
    This class is reliable and accurate because QueryPerformanceCounter is. Basically, it is like placing the QueryPerformanceCounter etc. calls right in your code. CPerftimer is just much easier to use then the direct calls.
     
    If anyone finds anything wrong with the class, please let me know.
     

    GeneralVery useful... Pinsusskabir5-Sep-00 0:37 

    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 | Mobile
    Web01 | 2.8.140916.1 | Last Updated 28 Apr 2000
    Article Copyright 2000 by Dean Wyant
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid