This paper summarizes the time functions commonly used in Windows platform, including 5 methods with precision of seconds, milliseconds and three of microseconds. It is divided into two types of time () and clock (), standard C/C + +, so the time () and clock () can be used not only in Windows systems but also in Linux systems. Under Windows System Three, use the API interface provided by Windows timeGetTime (), GetTickCount (), and QueryPerformanceCounter () to complete. At the end of this article, we give 5 sample code of timing methods.
Two chronograph functions in standard C/A + + time () and clock ()
time_t time (time_t *timer);
Returns the number of seconds elapsed at this point in time, from January 1, 1970 00:00:00 to now, at GMT (GMT).
time_t is actually a long-length typedef long time_t;
Header files: #include <time.h>
clock_t clock (void);
Returns the number of CPU clock ticks (clock ticks) that were passed when the process started to invoke the function, called the Wall Clock Time (Wal-clock), in milliseconds, in MSDN.
Clock_t is actually a long-length typedef long clock_t;
Header files: #include <time.h>
Windows System API functions
timeGetTime (), GetTickCount () and QueryPerformanceCounter ()
DWORD timeGetTime (VOID);
Returns the system time, in milliseconds. The system time is the number of milliseconds that have elapsed since the system was booted to the calling function. Note that this value is 32-bit and will loop between 0 and 2^32 for about 49.71 days.
Header files: #include <Mmsystem.h>
Reference Library: #pragma comment (lib, "Winmm.lib")
DWORD WINAPI GetTickCount (void);
This function, like timeGetTime (), also returns the system time, in milliseconds.
Header file: Direct use of # include <windows.h> is possible.
High-precision timing, in microseconds (1 milliseconds =1000 microseconds).
See the definition of two functions first
BOOL QueryPerformanceCounter (Large_integer *lpperformancecount);
Gets the value of the high-precision timer (if such a timer exists).
BOOL QueryPerformanceFrequency (Large_integer *lpfrequency);
Returns the frequency of the hardware-supported high-precision counters (times per second) and returns 0 for failure.
And look at Large_integer.
It is actually a consortium that can get __int64 quadpart, or you can get a low 32-bit DWORD LowPart and a high 32-bit value of long highpart respectively.
When used, the frequency of the counter is obtained first using QueryPerformanceFrequency (), and then the difference of the timer value obtained from the two calls to QueryPerformanceCounter () is calculated, and the accurate timing is obtained by using the difference removal frequency.
Header file: Direct use of # include <windows.h> is possible.
Here is an example code that can be tested on your computer.
[CPP]View Plaincopy
- Windows system time (), clock (), timeGetTime (), GetTickCount (), QueryPerformanceCounter () to chronograph by morewindows
- #include <stdio.h>
- #include <windows.h>
- #include <time.h>//time_t time () clock_t clock ()
- #include <Mmsystem.h>//timegettime ()
- #pragma comment (lib, "Winmm.lib")//timegettime ()
- int main ()
- {
- //Use time () to tick seconds
- time_t Timebegin, timeend;
- Timebegin = time (NULL);
- Sleep (1000);
- Timeend = time (NULL);
- printf ("%d\n", Timeend-timebegin);
- //Clock () for milliseconds
- clock_t Clockbegin, clockend;
- Clockbegin = Clock ();
- Sleep (800);
- Clockend = Clock ();
- printf ("%d\n", Clockend-clockbegin);
- //Use timeGetTime () to time milliseconds
- DWORD Dwbegin, dwend;
- Dwbegin = timeGetTime ();
- Sleep (800);
- Dwend = timeGetTime ();
- printf ("%d\n", Dwend-dwbegin);
- //Use GetTickCount () to time milliseconds
- DWORD Dwgtcbegin, dwgtcend;
- Dwgtcbegin = GetTickCount ();
- Sleep (800);
- Dwgtcend = GetTickCount ();
- printf ("%d\n", Dwgtcend-dwgtcbegin);
- //Use QueryPerformanceCounter () to time microseconds
- Large_integer Large_interger;
- double DFF;
- __int64 C1, C2;
- QueryPerformanceFrequency (&large_interger);
- DFF = Large_interger. QuadPart;
- QueryPerformanceCounter (&large_interger);
- C1 = Large_interger. QuadPart;
- Sleep (800);
- QueryPerformanceCounter (&large_interger);
- C2 = Large_interger. QuadPart;
- printf ("native high-precision timer frequency%lf\n", DFF);
- printf ("First timer value%I64D second timer value%i64d timer difference%i64d\n", C1, c2, C2-C1);
- printf ("Chronograph%lf ms \ n", (C2-C1) * 1000/DFF);
- printf ("by morewindows\n");
- return 0;
- }
Here is the test results on my computer:
Summary of various Windows clocking functions