Windows has a multimedia clock function:
TimegetTime
.
DWORD TIMEGETIME (VOID);
The unit is in milliseconds, the accuracy is relatively high, and "MMSystem.h" file is required when used, and "WinMM.LIB" file is added to the project.
In addition, Windows also provides the QueryPerformanceCounter function to achieve high-precision current clock cycles, so you can use this function to write a time-console function than the TimegetTime precision.
/ / Get the number of clock cycles in the CPU per second
__inline longlong getsecondcount ()
{
Static Large_integer licounter = {0};
IF (0 == Licounter.quadpart)
QueryPerformanceFrequency (& LiCounter);
Return licounter.quadpart;
}
// Return to the current time unit: milliseconds
__INline DWORD HIGHGETTIME ()
{
Large_integer licurrent = {0};
QueryperFormanceCounter (& licurrent);
Return (DWORD) (Licurrent.quadpart * 1000 / GetSecondcount ());
}
In order to test the accuracy of each clock function, the author wrote the following test procedure:
A gettickcount ()
#include "conoe.h"
#include "windows.h"
void main ()
{
Sleep (1000);
DWORD OLD_TIME = GetTickCount ();
For (int i = 0; i <10; i)
{
INT n = 0;
For (int J = 0; J <500000; J)
{
IF (j% 10 == 0)
n--;
Else
N ;
}
DWORD TIME = GetTickCount ();
DWORD DT = TIME - OLD_TIME;
Old_time = TIME;
Printf ("n =% D time consumption:% D / N", N, DT);
}
_Getch ();
}
Output results:
N = 400000 time consumption: 0
N = 400000 time consumption: 16
N = 400000 time consumption: 0
N = 400000 time consuming: 15
N = 400000 time consumption: 16
N = 400000 time consumption: 0
N = 400000 time consumption: 16
N = 400000 time consumption: 0
N = 400000 time consuming: 15
N = 400000 time consumption: 16
Two TimegetTime ()
#include "conoe.h"
#include "windows.h"
#include "mmsystem.h"
#pragma Comment (Lib, "Winmm.Lib") Void Main ()
{
Sleep (1000);
DWORD OLD_TIME = TIMEGETTIME ();
For (int i = 0; i <10; i)
{
INT n = 0;
For (int J = 0; J <500000; J)
{
IF (j% 10 == 0)
n--;
Else
N ;
}
DWORD TIME = TimegetTime ();
DWORD DT = TIME - OLD_TIME;
Old_time = TIME;
Printf ("n =% D time consumption:% D / N", N, DT);
}
_Getch ();
}
Output results:
N = 400000 time consumption: 0
N = 400000 time consuming: 15
N = 400000 time consumption: 16
N = 400000 time consuming: 15
N = 400000 time consumption: 16
N = 400000 time consumption: 16
N = 400000 time consuming: 15
N = 400000 time consumption: 0
N = 400000 time consumption: 16
N = 400000 time consumption: 16
Three highgettime ()
#include "conoe.h"
#include "windows.h"
__inline longlong getsecondcount ()
{
Static Large_integer licounter = {0};
IF (0 == Licounter.quadpart)
QueryPerformanceFrequency (& LiCounter);
Return licounter.quadpart;
}
// Return to the current time unit: milliseconds
__INline DWORD HIGHGETTIME ()
{
Large_integer licurrent = {0};
QueryperFormanceCounter (& licurrent);
Return (DWORD) (Licurrent.quadpart * 1000 / GetSecondcount ());
}
void main ()
{
Sleep (1000);
DWORD OLD_TIME = HIGHGETTIME ();
For (int i = 0; i <10; i)
{
INT n = 0;
For (int J = 0; J <500000; J)
{
IF (j% 10 == 0)
n--;
Else
N ;
}
DWORD TIME = HIGHGETTIME ();
DWORD DT = TIME - OLD_TIME;
Old_time = TIME;
Printf ("n =% D time consumption:% D / N", N, DT);
}
_Getch ();
}
Output results:
N = 400000 time consumption: 10
N = 400000 time consumption: 9
N = 400000 time consumption: 10
n = 400000 time consumption: 10n = 400000 time consumption: 9
N = 400000 time consumption: 10
N = 400000 time consumption: 10
N = 400000 time consumption: 10
N = 400000 time consumption: 10
N = 400000 time consumption: 10
All three test procedures were completed 10 times, then each time spent time should be different, according to the result of the program output, we can see that TimegetTime is high than gettickcount accuracy, the highest level of highgettime.