uint64_t getCurrentTime1()
{
struct timeb rawtime;
ftime(&rawtime);
return rawtime.time * 1000 + rawtime.millitm;
}
uint64_t getCurrentTime2()
{
return GetTickCount64();
}
uint64_t getCurrentTime3()
{
uint64_t current_tics;
FILETIME ct;
GetSystemTimeAsFileTime(&ct);
current_tics = (unsigned __int64)ct.dwLowDateTime +(((unsigned __int64)ct.dwHighDateTime) << 32);
return current_tics;
}
unsigned int day[] = {0,31,59,90,120,151,181,212,243,273,304,334 };
uint64_t getCurrentTime4()
{
LARGE_INTEGER count, freq;
BOOL br = QueryPerformanceCounter(&count);
br = QueryPerformanceFrequency(&freq);
return count.QuadPart * 1000000000 / freq.QuadPart;
}
uint64_t getCurrentTime5()
{
SYSTEMTIME ltime;
GetLocalTime(<ime);
uint64_t retmilsec = ((ltime.wYear - 1970) * 365 + ((ltime.wYear - 1970) / 4 - 1) + day[ltime.wMonth - 1] + ltime.wDay - 1) * 86400 + ltime.wHour * 3600 + ltime.wMinute * 60 + ltime.wSecond;
retmilsec = retmilsec * 1000 + ltime.wMilliseconds;
return retmilsec;
}
int main()
{
uint64_t starttime = GetTickCount64();
for (int i = 0 ; i < 200000000;i++)
{
getCurrentTime1();
}
uint64_t endtime = GetTickCount64();
cout<<"cost time(ms):\t" << endtime-starttime << "\t--->ftime" << endl;
starttime = GetTickCount64();
for (int i = 0; i < 200000000; i++)
{
getCurrentTime2();
}
endtime = GetTickCount64();
cout<<"cost time(ms):\t" << endtime - starttime<<"\t--->GetTickCount64" << endl;
starttime = GetTickCount64();
for (int i = 0; i < 200000000; i++)
{
getCurrentTime3();
}
endtime = GetTickCount64();
cout<<"cost time(ms):\t" << endtime - starttime << "\t--->GetSystemTimeAsFileTime" << endl;
starttime = GetTickCount64();
for (int i = 0; i < 200000000; i++)
{
getCurrentTime4();
}
endtime = GetTickCount64();
cout<<"cost time(ms):\t" << endtime - starttime<<"\t--->QueryPerformanceCounter" << endl;
starttime = GetTickCount64();
for (int i = 0; i < 200000000; i++)
{
getCurrentTime5();
}
endtime = GetTickCount64();
cout<< "cost time(ms):\t" << endtime - starttime << "\t--->GetLocalTime" << endl;
}
输出结果:
cost time(ms): 32230 --->ftime
cost time(ms): 6490 --->GetTickCount64
cost time(ms): 8128 --->GetSystemTimeAsFileTime
cost time(ms): 44226 --->QueryPerformanceCounter
cost time(ms): 28065 --->GetLocalTime