Click here to Skip to main content
Click here to Skip to main content

Precise duration measurement

By , 27 Dec 1999
 
  • 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
    No Biography provided

    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

     
    Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    QuestionlicensememberJohnWallis4210 Jun '09 - 17:40 
    AnswerRe: licensememberLaurent.Guinnard1 Nov '09 - 7:01 
    Questionhow come it wont compile?memberchefmannyd16 May '09 - 8:05 
    AnswerRe: how come it wont compile?memberDave Cross20 Jul '09 - 0:49 
    QuestionCan I write my own profilling codememberaamerqureshi12 Mar '07 - 1:02 
    GeneralTerrific.. but could use a few small functionsmemberhannahb3 Aug '06 - 6:14 
    Questionseconds or milliseconds ?memberStlan25 Apr '05 - 3:55 
    AnswerRe: seconds or milliseconds ?memberStlan25 Apr '05 - 3:56 
    Oops sorry, I should clean my glasses!
    I read milli- instead of micro- Dead | X|
    GeneralExcellentmemberSimon Hughes2 Jun '04 - 1:56 
    GeneralThe best!memberDlt7520 May '04 - 20:21 
    GeneralAccurate timingmemberBernhard Hofmann9 Sep '03 - 21:59 
    GeneralCalling Start() Causes a Thread SwitchmemberMikeAThon20 Feb '03 - 15:08 
    GeneralRe: Calling Start() Causes a Thread Switchmemberpeterchen25 Apr '05 - 6:08 
    GeneralRe: Calling Start() Causes a Thread SwitchmemberToby Opferman15 Aug '06 - 16:39 
    General5/5memberRalph Wetzel8 Jan '03 - 10:42 
    GeneralExcellent!memberNitron12 Nov '02 - 8:16 
    GeneralSimple and concise I like it.memberAnonymous4 Mar '02 - 9:46 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Permalink | Advertise | Privacy | Mobile
    Web03 | 2.6.130516.1 | Last Updated 28 Dec 1999
    Article Copyright 1999 by Laurent Guinnard
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid