The Code Project View our sponsorsClick here for the free Dundas Upload controlAdvertise on the CodeProject
Home >> Date / Time handling >> General

The CPerfTimer timer class
By Dean Wyant

This class encapsulates QueryPerformanceCounter for high precision timing 
 VC6, Win9x, NT4
 Posted 12 Apr 2000
 Updated 28 Apr 2000
Articles by this author
Send to a friend
Printer friendly version
Lounge New Articles Sign in Forums Contribute
Broken links? Email us!
16 users have rated this article. result:
4.31 out of 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.

    [Top] Sign in to vote for this article:     PoorExcellent  
    Hint: For improved responsiveness, use Internet Explorer 4 (or above) with Javascript enabled, choose 'Use DHTML' from the View dropdown and hit 'Set Options'.
     Keyword Filter
     View   Per page   Messages since
    New threadMessages 1 to 6 of 6 (Total: 6)First Prev Next Last
    Subject 
    Author 
    Date 
      Timer runs to quick
    Rob Ottenhoff 6:01 5 Mar 01 
      Re: Timer runs to quick
    Anonymous 12:04 5 Mar 01 
      Usage Notes
    Dean 17:15 27 Jan 01 
      Very useful...
    kabir 6:37 5 Sep 00 
      The poor rating was due to compile problem - update should be posted soon
    Dean Wyant 4:44 15 Apr 00 
      Updated posted
    Chris Maunder 4:50 15 Apr 00 
    Last Visit: 12:00 Friday 1st January, 1999First Prev Next Last

    Home >> Date / Time handling >> General
    last updated 28 Apr 2000
    Article content copyright Dean Wyant, 2000
    everything else © CodeProject, 1999-2001.
    The Code Project View our sponsorsClick here for Dundas Software - 3D accelerated presentation quality ASP charting Advertise on the CodeProject