Click here to Skip to main content
13,802,317 members
Click here to Skip to main content
Add your own
alternative version


43 bookmarked
Posted 27 Dec 1999

Precise duration measurement

, 27 Dec 1999
Rate this:
Please Sign up or sign in to vote.
A simple class that provides high precision timing.
  • Download demo project - 5 Kb
  • Download source files - 1 Kb
  • <!-- Article Starts -->

    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;
    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;
    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



    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
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    Questionlicense Pin
    JohnWallis4210-Jun-09 18:40
    memberJohnWallis4210-Jun-09 18:40 
    AnswerRe: license Pin
    norton4211-Nov-09 8:01
    membernorton4211-Nov-09 8:01 
    Questionhow come it wont compile? Pin
    chefmannyd16-May-09 9:05
    memberchefmannyd16-May-09 9:05 
    AnswerRe: how come it wont compile? Pin
    Dave Cross20-Jul-09 1:49
    professionalDave Cross20-Jul-09 1:49 
    QuestionCan I write my own profilling code Pin
    aamerqureshi12-Mar-07 2:02
    memberaamerqureshi12-Mar-07 2:02 
    GeneralTerrific.. but could use a few small functions Pin
    hannahb3-Aug-06 7:14
    memberhannahb3-Aug-06 7:14 
    Questionseconds or milliseconds ? Pin
    Stlan25-Apr-05 4:55
    memberStlan25-Apr-05 4:55 
    AnswerRe: seconds or milliseconds ? Pin
    Stlan25-Apr-05 4:56
    memberStlan25-Apr-05 4:56 
    GeneralExcellent Pin
    Simon Hughes2-Jun-04 2:56
    memberSimon Hughes2-Jun-04 2:56 
    GeneralThe best! Pin
    Dlt7520-May-04 21:21
    memberDlt7520-May-04 21:21 
    GeneralAccurate timing Pin
    Bernhard Hofmann9-Sep-03 22:59
    memberBernhard Hofmann9-Sep-03 22:59 
    GeneralCalling Start() Causes a Thread Switch Pin
    Mike O'Neill20-Feb-03 16:08
    memberMike O'Neill20-Feb-03 16:08 
    GeneralRe: Calling Start() Causes a Thread Switch Pin
    peterchen25-Apr-05 7:08
    memberpeterchen25-Apr-05 7:08 
    GeneralRe: Calling Start() Causes a Thread Switch Pin
    Toby Opferman15-Aug-06 17:39
    memberToby Opferman15-Aug-06 17:39 
    General5/5 Pin
    Ralph Wetzel8-Jan-03 11:42
    memberRalph Wetzel8-Jan-03 11:42 
    GeneralExcellent! Pin
    Nitron12-Nov-02 9:16
    memberNitron12-Nov-02 9:16 
    GeneralSimple and concise I like it. Pin
    4-Mar-02 10:46
    suss4-Mar-02 10:46 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Cookies | Terms of Use | Mobile
    Web03 | 2.8.181215.1 | Last Updated 28 Dec 1999
    Article Copyright 1999 by Laurent Guinnard
    Everything else Copyright © CodeProject, 1999-2018
    Layout: fixed | fluid