CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Timer in C++

  1. #1
    Join Date
    Apr 2009
    Posts
    27

    Timer in C++

    Code:
    #include<iostream>
    #include<time.h>
    using namespace std;
    
    
    int main(void){
    	clock_t start, finish;
    	start=clock();
    	cout<<start<<endl;
    	cin.get();
    	finish=clock();
    	cout<<finish<<endl;
    	cin.get();
    }
    The above code displays outputs similar to:
    Code:
    0
    
    468
    It depends on how fast I hit the return key, of course. My question is: what is the unit of time displayed? I am guessing it to be milliseconds.

  2. #2
    Join Date
    Jun 2005
    Posts
    315

    Re: Timer in C++

    maybe this link may help.

  3. #3
    Join Date
    Aug 2008
    Posts
    902

    Re: Timer in C++

    Clock is more portable, but my experience is that it's not very accurate.

    I usually use GetTickCount instead, which is usually 10-20ms accuracy on most modern systems. If you need greater accuracy, you can use timeGetTime as well.

  4. #4
    Join Date
    Aug 1999
    Location
    Darmstadt, FRG
    Posts
    87

    Re: Timer in C++

    Assuming you are interested in getting the time with high resolution the functions QueryPerformanceCounter and QueryPerformanceFrequency may be worth to be considered.

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Timer in C++

    not that as you're doing it now. part of your timing includes the time needed to output the start time to the console.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured