Windows Compute program Run time (high precision timing)

Source: Internet
Author: User

First, meet the Clock () and GetTickCount ():

One, clock ()
Clock () is a timing function in C/s + +, and its associated data type is clock_t. In MSDN, the check for the clock function is defined as follows:
clock_t clock (void);
In simple terms, it is the time that the program consumes the CPU from startup to function calls. This function returns the number of CPU clock ticks (clock ticks) from "Open this program process" to "call clock () function in program", called Wall Clock Time (Wal-clock) in MSDN, and returns 1 if the wall clock time is not desirable. Where clock_t is the data type used to hold the time.

In the time.h file, we can find the definition of it:

[CPP]View PlainCopy
    1. #ifndef _clock_t_defined
    2. typedef LONG clock_t;
    3. #define _clock_t_defined
    4. #endif

Clock_t is a long shaping number. In the Time.h file, you also define a constant clocks_per_sec, which is used to indicate how many clock units are in a second, defined as follows:
#define CLOCKS_PER_SEC ((clock_t) 1000)

You can see that every 1 per thousand seconds (1 milliseconds), the value returned by the call to the clock () function increases by 1. Simply put, the clock () can be accurate to 1 milliseconds.
Under the Linux system, the value of the clocks_per_sec may be different, the current use of the Linux print out the value is 1000000, indicating a microsecond. This is a point to note.

Second, GetTickCount ()
GetTickCount function: It returns the number of milliseconds that have elapsed from the operating system boot to the current, and its return value is DWORD. Often used to determine when a method executes, its function prototype is a DWORD GetTickCount (void), and the return value is stored in a 32-bit double-word type DWORD, so the maximum value that can be stored is (2^32-1) MS is approximately 49.71 days, so if the system is running for more than 49.71 days, this number will be referred to in 0,MSDN as well: "Retrieves the numbers of milliseconds that has elapsed since the System was started, up to 49.7 days. ". Therefore, if you are writing server-side programs, you must be extremely careful here to avoid causing unexpected conditions.
Special Note: This function is not sent in real time, but is sent by the system every 18ms, so its minimum precision is 18ms. When a precision calculation of less than 18ms is required, the stopwatch method should be used.

Use clock to calculate the time difference:

[CPP]View PlainCopy
  1. #include <time.h>
  2. #include <stdio.h>
  3. int main ()
  4. {
  5. double start,end,cost;
  6. Start=clock ();
  7. Sleep (1);
  8. End=clock ();
  9. Cost=end-start;
  10. printf ("%f/n", cost);
  11. return 0;
  12. }

Use GetTickCount to calculate the time difference:

[CPP]View PlainCopy
  1. #include <iostream>
  2. #include <windows.h>
  3. Using namespace std;
  4. int main ()
  5. {
  6. Double start = GetTickCount ();
  7. Sleep (1000);
  8. double End=gettickcount ();
  9. cout << "GetTickCount:" << end-start << Endl;
  10. return 0;
  11. }

It can be concluded that clock is more accurate than gettickcount, so it is recommended to use clock if a higher precision time difference is required. When the program time-consuming statistics, you can use clock to help complete the time difference calculation.

Third, if the need for higher time accuracy (such as server program time-consuming statistics), you can start the timing statistics before the call QueryPerformanceFrequency () function to obtain the clock frequency of the machine internal timer, Then QueryPerformanceCounter () is called before and after the event that requires strict timing, and the exact time of the event (the precision can reach microsecond level) can be calculated using the difference in counts and the clock frequency of two times. Here is the sample code:

[CPP]View PlainCopy
  1. #include <windows.h>
  2. #include <iostream>
  3. int main ()
  4. {
  5. Large_integer Timestart; //Start time
  6. Large_integer Timeend; //End time
  7. Large_integer frequency; //Timer frequency
  8. QueryPerformanceFrequency (&frequency);
  9. Double QuadPart = (double) frequency. QuadPart; //Timer frequency
  10. QueryPerformanceCounter (&timestart);
  11. Sleep (1000); //Delay one second
  12. QueryPerformanceCounter (&timeend);
  13. //Get two time-consuming
  14. double elapsed = (Timeend.quadpart-timestart.quadpart)/QuadPart;
  15. Std::cout << elapsed << Std::endl; //unit is seconds, precision is microseconds (1000000/cpu frequency)
  16. System ("pause");
  17. return 0;
  18. }

Run a few more times, you can see that each sleep time spent is different, so sleep time accuracy is very low!

51330028

Windows Compute program Run time (high precision timing)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.