Click here to Skip to main content
15,888,803 members
Articles / Programming Languages / C++
Article

Precise duration measurement

Rate me:
Please Sign up or sign in to vote.
4.11/5 (25 votes)
27 Dec 1999 120.5K   2.7K   44   17
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


    Written By
    Switzerland Switzerland
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    Questionlicense Pin
    JohnWallis4210-Jun-09 17:40
    JohnWallis4210-Jun-09 17:40 
    AnswerRe: license Pin
    norton4211-Nov-09 7:01
    norton4211-Nov-09 7:01 
    Questionhow come it wont compile? Pin
    chefmannyd16-May-09 8:05
    chefmannyd16-May-09 8:05 
    AnswerRe: how come it wont compile? Pin
    Dave Cross20-Jul-09 0:49
    professionalDave Cross20-Jul-09 0:49 
    QuestionCan I write my own profilling code Pin
    aamerqureshi12-Mar-07 1:02
    aamerqureshi12-Mar-07 1:02 
    GeneralTerrific.. but could use a few small functions Pin
    hannahb3-Aug-06 6:14
    hannahb3-Aug-06 6:14 
    Questionseconds or milliseconds ? Pin
    Stlan25-Apr-05 3:55
    Stlan25-Apr-05 3:55 
    AnswerRe: seconds or milliseconds ? Pin
    Stlan25-Apr-05 3:56
    Stlan25-Apr-05 3:56 
    GeneralExcellent Pin
    Simon Hughes2-Jun-04 1:56
    Simon Hughes2-Jun-04 1:56 
    GeneralThe best! Pin
    Dlt7520-May-04 20:21
    Dlt7520-May-04 20:21 
    GeneralAccurate timing Pin
    Bernhard Hofmann9-Sep-03 21:59
    Bernhard Hofmann9-Sep-03 21:59 
    Hi
    A long time ago (Feb. 1999), I read an article by Chih-Hao Tsai of the University of Illinois at Urbana-Champaign.

    He was using Windows 95, so the high res timer wasn't available. Also, if anyone needs a precise timer on an older OS, they might be interested in the following. He used the multi-media timer (in winmm.lib) to get a millisecond accurate timer.

    His article is unfortunately no longer where it once was (http://casper.beckman.uiuc.edu/~c-tsai4/cogsci/millisecond.html). His summary was as follows:

    "Virtually all cognitive psychology experiments require a millisecond resolution timing routine. In the good old MS-DOS days, it was easy to achieve this by increasing the frequency of hardware interrupt IRQ0 (int 8h), at the cost of increased instability of the whole system. Today, most people have already moved from MS-DOS to Windows 95 operating system. Since Windows 95 a such a complex system, in general a programmer cannot (or is not allowed to) intrude into lower levels of the system.

    In fact, both the standard C run-time library and the Win32 API provide some timing functions. But we do not know how accurate they are. Two run-time library functions and two Win32 API functions from Microsoft Visual C++ Version 4.0 were tested on a Windows 95 machine with a standardized procedure for their quality (in terms of the resolution they can achieve). It was found that the two run-time library functions were basically at the same precision level: 18.2 Hz (resolution = 55 ms). The two Win32 API functions performed better than the run-time library functions, but only one of them reached near-millisecond resolution. It was concluded that only the Multimedia Timer is qualified to be used in psychological experiments.
    "

    I still have the code samples he wrote to test the timers as well if anyone's interested.
    GeneralCalling Start() Causes a Thread Switch Pin
    Mike O'Neill20-Feb-03 15:08
    Mike O'Neill20-Feb-03 15:08 
    GeneralRe: Calling Start() Causes a Thread Switch Pin
    peterchen25-Apr-05 6:08
    peterchen25-Apr-05 6:08 
    GeneralRe: Calling Start() Causes a Thread Switch Pin
    Toby Opferman15-Aug-06 16:39
    Toby Opferman15-Aug-06 16:39 
    General5/5 Pin
    Ralph Wetzel8-Jan-03 10:42
    Ralph Wetzel8-Jan-03 10:42 
    GeneralExcellent! Pin
    Nitron12-Nov-02 8:16
    Nitron12-Nov-02 8:16 
    GeneralSimple and concise I like it. Pin
    4-Mar-02 9:46
    suss4-Mar-02 9: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.