Click here to Skip to main content
Licence 
First Posted 27 Dec 1999
Views 87,240
Bookmarked 38 times

Precise duration measurement

By | 27 Dec 1999 | Article
A simple class that provides high precision timing.
  • Download demo project - 5 Kb
  • Download source files - 1 Kb
  • Here is a simple class used to measure the duration of functions or code parts (from some microseconds to milliseconds) using the Windows high precision timer.

    This class is very easy to use. The following code measure the duration of the Foo function:

        CDuration timer;
    
        timer.Start();
        Foo();
        timer.Stop();
    
        cout << "Foo duration: " << timer.GetDuration()/1000.0 << " milliseconds" << endl;
    

    The GetDuration() method returns the number of microseconds between calls to Start() and Stop(). This value depends on the precision of the internal timer. In a multitasking operating system like Windows, it is difficult to make very accurate duration measurement. Thus, measurement less than a few milliseconds will be correct, but longer measurement could have a difference of a tenth of a millisecond or more because of tasks switching.

    The constructor implements a calibration procedure so the following code will return 0 milliseconds:

        CDuration dur;
    
        dur.Start();
        dur.Stop();
    
        cout << "Zero duration: " << dur.GetDuration()/1000.0 << " milliseconds" << endl;
    

    If you really need no task switching during a long time, you could increment the process and thread priority in the Start() method and decrement it in Stop() .

    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

    Laurent Guinnard



    Switzerland Switzerland

    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
    Questionlicense PinmemberJohnWallis4217:40 10 Jun '09  
    AnswerRe: license PinmemberLaurent.Guinnard7:01 1 Nov '09  
    Questionhow come it wont compile? Pinmemberchefmannyd8:05 16 May '09  
    AnswerRe: how come it wont compile? PinmemberDave Cross0:49 20 Jul '09  
    QuestionCan I write my own profilling code Pinmemberaamerqureshi1:02 12 Mar '07  
    GeneralTerrific.. but could use a few small functions Pinmemberhannahb6:14 3 Aug '06  
    Questionseconds or milliseconds ? PinmemberStlan3:55 25 Apr '05  
    AnswerRe: seconds or milliseconds ? PinmemberStlan3:56 25 Apr '05  
    GeneralExcellent PinmemberSimon Hughes1:56 2 Jun '04  
    GeneralThe best! PinmemberDlt7520:21 20 May '04  
    GeneralAccurate timing PinmemberBernhard Hofmann21:59 9 Sep '03  
    GeneralCalling Start() Causes a Thread Switch PinmemberMikeAThon15:08 20 Feb '03  
    GeneralRe: Calling Start() Causes a Thread Switch Pinmemberpeterchen6:08 25 Apr '05  
    GeneralRe: Calling Start() Causes a Thread Switch PinmemberToby Opferman16:39 15 Aug '06  
    1. Don't use "Sleep", use "SwitchToThread" it's a much lighter weight method of yeilding.
    2. The IDEA is to give up your time slice and it has NOTHING to do whether your application is multithreaded or not. Any thread on the system can take the time slice.
    3. You can set your thread to be high priority or real time priority to prevent interruption. However there are still things called "interrupts" that can occur and obscure your timings, so definately take lots of measurements.
     

     
    8bc7c0ec02c0e404c0cc0680f7018827ebee

    General5/5 PinmemberRalph Wetzel10:42 8 Jan '03  
    GeneralExcellent! PinmemberNitron8:16 12 Nov '02  
    GeneralSimple and concise I like it. PinmemberAnonymous9:46 4 Mar '02  

    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
    Web01 | 2.5.120529.1 | Last Updated 28 Dec 1999
    Article Copyright 1999 by Laurent Guinnard
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid