Click here to Skip to main content
6,595,444 members and growing! (18,026 online)
Email Password   helpLost your password?
Languages » C / C++ Language » General     Intermediate

Precise duration measurement

By Laurent Guinnard

A simple class that provides high precision timing.
VC6, Dev
Posted:27 Dec 1999
Views:71,658
Bookmarked:33 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
43 votes for this article.
Popularity: 6.90 Rating: 4.22 out of 5
1 vote, 4.5%
1
1 vote, 4.5%
2
2 votes, 9.1%
3
2 votes, 9.1%
4
16 votes, 72.7%
5
  • 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


    Member

    Location: Switzerland Switzerland

    Other popular C / C++ Language articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
    Questionlicense PinmemberJohnWallis4218:40 10 Jun '09  
    AnswerRe: license PinsupporterLaurent.Guinnard8:01 1 Nov '09  
    Generalhow come it wont compile? Pinmemberchefmannyd9:05 16 May '09  
    GeneralRe: how come it wont compile? PinmemberDave Cross1:49 20 Jul '09  
    QuestionCan I write my own profilling code Pinmemberaamerqureshi2:02 12 Mar '07  
    GeneralTerrific.. but could use a few small functions Pinmemberhannahb7:14 3 Aug '06  
    Generalseconds or milliseconds ? PinmemberStlan4:55 25 Apr '05  
    GeneralRe: seconds or milliseconds ? PinmemberStlan4:56 25 Apr '05  
    GeneralExcellent PinmemberSimon Hughes2:56 2 Jun '04  
    GeneralThe best! PinmemberDlt7521:21 20 May '04  
    GeneralAccurate timing PinmemberBernhard Hofmann22:59 9 Sep '03  
    GeneralCalling Start() Causes a Thread Switch PinmemberMikeAThon16:08 20 Feb '03  
    GeneralRe: Calling Start() Causes a Thread Switch Pinsupporterpeterchen7:08 25 Apr '05  
    GeneralRe: Calling Start() Causes a Thread Switch PinmemberToby Opferman17:39 15 Aug '06  
    General5/5 PinmemberRalph Wetzel11:42 8 Jan '03  
    GeneralExcellent! PinmemberNitron9:16 12 Nov '02  
    GeneralSimple and concise I like it. PinmemberAnonymous10:46 4 Mar '02  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 27 Dec 1999
    Editor: Chris Maunder
    Copyright 1999 by Laurent Guinnard
    Everything else Copyright © CodeProject, 1999-2009
    Web17 | Advertise on the Code Project