Click here to Skip to main content
6,630,901 members and growing! (18,845 online)
Email Password   helpLost your password?
General Programming » Date and Time » General     Intermediate

The CPerfTimer timer class

By Dean Wyant

This class encapsulates QueryPerformanceCounter for high precision timing
VC6, Visual Studio, Dev
Posted:11 Apr 2000
Updated:27 Apr 2000
Views:131,403
Bookmarked:40 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
42 votes for this article.
Popularity: 6.93 Rating: 4.27 out of 5
3 votes, 14.3%
1
1 vote, 4.8%
2

3

4
17 votes, 81.0%
5
  • 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


    Member

    Occupation: Web Developer
    Location: United States United States

    Other popular Date and Time articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 25 of 51 (Total in Forum: 51) (Refresh)FirstPrevNext
    GeneralQueryPerformanceCounter fails sometimes, when structmember alignment is set to 1 byte Pinmemberhund120005:42 20 Nov '08  
    GeneralRe: QueryPerformanceCounter fails sometimes, when structmember alignment is set to 1 byte PinmemberDean Wyant6:39 20 Nov '08  
    QuestionApplication Error When Calling Start(TRUE) Pinmembershizulooi0:08 31 Oct '07  
    AnswerRe: Application Error When Calling Start(TRUE) PinmemberDean Wyant4:04 31 Oct '07  
    Questionproblem compiling CPerfTimer on VC++ Express Pinmemberabarund14:08 27 Aug '06  
    AnswerRe: problem compiling CPerfTimer on VC++ Express PinmemberDean Wyant5:30 28 Aug '06  
    GeneralPossible bug Pinmembermetimmee13:39 7 May '06  
    GeneralRe: Possible bug PinmemberDean Wyant6:08 12 May '06  
    GeneralLaptop Pinmemberzvivered8:11 30 Mar '05  
    GeneralRe: Laptop PinmemberDean Wyant13:21 30 Mar '05  
    GeneralWindows CE PinmemberGrimoire13:31 2 Nov '04  
    GeneralExcellent Pinmemberthought2:48 20 Jan '04  
    GeneralNice class. This saved me a lot of time. Pinmembervarnk11:06 19 Sep '03  
    GeneralRe: Nice class. This saved me a lot of time. PinmemberDean Wyant4:13 31 Oct '07  
    GeneralWhy not use critical sections? PinmemberBrian van der Beek6:27 24 Jun '03  
    GeneralRe: Why not use critical sections? PinmemberDean Wyant18:04 25 Jun '03  
    GeneralCPerfTimer under VC++? PinmemberDarkGuardian4:04 5 Jun '03  
    GeneralRe: CPerfTimer under VC++? PinmemberDean Wyant5:11 5 Jun '03  
    GeneralRe: CPerfTimer under VC++? PinmemberDarkGuardian20:24 5 Jun '03  
    GeneralRe: CPerfTimer under VC++? PinmemberDarkGuardian20:37 5 Jun '03  
    GeneralRe: CPerfTimer under VC++? Pinmemberbitkidoku16:05 26 Sep '06  
    Generalmissing stdafx.h Pinmembersteven76512:52 12 May '03  
    GeneralRe: missing stdafx.h PinmemberDean Wyant15:23 13 May '03  
    Generalmissing stdafx.h Pinmembersteven76512:52 12 May '03  
    GeneralOn multiprocessor machine Pinmemberwarmcold13:33 4 May '03  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 27 Apr 2000
    Editor: Chris Maunder
    Copyright 2000 by Dean Wyant
    Everything else Copyright © CodeProject, 1999-2009
    Web17 | Advertise on the Code Project