Summarize the commonly used timing functions on Windows, including five methods with precision of second, millisecond, and microsecond. It can be divided into two types of time () and clock () under the Standard C/C ++, the time () and clock () used by the Standard C/C ++ () it can be used in both Windows and Linux systems.
In Windows, the following APIs are provided: timeGetTime (), GetTickCount (), and QueryPerformanceCounter.
[Cpp]
# Include <windows. h>
# Include <time. h> // time_t time () clock_t clock ()
# Include <Mmsystem. h> // timeGetTime ()
# Pragma comment (lib, "Winmm. lib") // timeGetTime ()
Int main (int argc, char * argv [])
{
// Use time () to time the second
Time_t timeBegin, timeEnd;
TimeBegin = time (NULL );
Sleep (800 );
TimeEnd = time (NULL );
Printf ("time % d \ n", timeEnd-timeBegin );
// Use clock () for Millisecond Time
Clock_t clockBegin, clockEnd;
ClockBegin = clock ();
Sleep (800 );
ClockEnd = clock ();
Printf ("clock % d \ n", clockEnd-clockBegin );
// Use timeGetTime () for millisecond timing
DWORD dwBegin, dwEnd;
DwBegin = timeGetTime ();
Sleep (800 );
DwEnd = timeGetTime ();
Printf ("timeGetTime % d \ n", dwEnd-dwBegin );
// Use GetTickCount () for Millisecond Time
DWORD dwGTCBegin, dwGTCEnd;
DwGTCBegin = GetTickCount ();
Sleep (800 );
DwGTCEnd = GetTickCount ();
Printf ("GetTickCount % d \ n", dwGTCEnd-dwGTCBegin );
// Use QueryPerformanceCounter () for microseconds
LARGE_INTEGER large_interger;
Double dff;
_ Int64 c1, c2;
// Obtain whether a precise timer is supported. Timer frequency
If (QueryPerformanceFrequency (& large_interger ))
{
Dff = large_interger.QuadPart;
// Obtain the timer Value
QueryPerformanceCounter (& large_interger );
C1 = large_interger.QuadPart;
Sleep (800 );
// Obtain the timer Value
QueryPerformanceCounter (& large_interger );
C2 = large_interger.QuadPart;
Printf ("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 ("Timing % lf millisecond \ n", (c2-c1) * 1000/dff );
Printf ("\ n ");
}
Return 0;
}
# Include <windows. h>
# Include <time. h> // time_t time () clock_t clock ()
# Include <Mmsystem. h> // timeGetTime ()
# Pragma comment (lib, "Winmm. lib") // timeGetTime ()
Int main (int argc, char * argv [])
{
// Use time () to time the second
Time_t timeBegin, timeEnd;
TimeBegin = time (NULL );
Sleep (800 );
TimeEnd = time (NULL );
Printf ("time % d \ n", timeEnd-timeBegin );
// Use clock () for Millisecond Time
Clock_t clockBegin, clockEnd;
ClockBegin = clock ();
Sleep (800 );
ClockEnd = clock ();
Printf ("clock % d \ n", clockEnd-clockBegin );
// Use timeGetTime () for millisecond timing
DWORD dwBegin, dwEnd;
DwBegin = timeGetTime ();
Sleep (800 );
DwEnd = timeGetTime ();
Printf ("timeGetTime % d \ n", dwEnd-dwBegin );
// Use GetTickCount () for Millisecond Time
DWORD dwGTCBegin, dwGTCEnd;
DwGTCBegin = GetTickCount ();
Sleep (800 );
DwGTCEnd = GetTickCount ();
Printf ("GetTickCount % d \ n", dwGTCEnd-dwGTCBegin );
// Use QueryPerformanceCounter () for microseconds
LARGE_INTEGER large_interger;
Double dff;
_ Int64 c1, c2;
// Obtain whether a precise timer is supported. Timer frequency
If (QueryPerformanceFrequency (& large_interger ))
{
Dff = large_interger.QuadPart;
// Obtain the timer Value
QueryPerformanceCounter (& large_interger );
C1 = large_interger.QuadPart;
Sleep (800 );
// Obtain the timer Value
QueryPerformanceCounter (& large_interger );
C2 = large_interger.QuadPart;
Printf ("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 ("Timing % lf millisecond \ n", (c2-c1) * 1000/dff );
Printf ("\ n ");
}
Return 0;
}