#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#if defined WINDOWS
#include <Windows.h>
#else
#include <sys/time.h>
#endif
using namespace std;
#if defined WINDOWS
LONGLONG get_curr_usec()
{
LARGE_INTEGER tm, freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&tm);
tm.QuadPart *= 1000000;
tm.QuadPart /= freq.QuadPart;
return tm.QuadPart;
}
#else
LONGLONG get_curr_usec()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000 + tv.tv_usec;
}
#endif
void test_clock()
{
auto time_point = chrono::system_clock::time_point();
auto system_start = chrono::system_clock::now();
long long start_time = get_curr_usec();
for (int i = 0; i < 20; i++)
{
auto now = std::chrono::system_clock::now();
auto curr_time = get_curr_usec();
auto diff = now - system_start;
auto micro_second1 = chrono::duration_cast<chrono::microseconds>(diff);
auto micro_second2 = curr_time - start_time;
printf("std micro_second:[%8lld], Performance micro_second:[%8lld]\n", micro_second1.count(), micro_second2);
}
}
int main()
{
test_clock();
return 0;
}
windows下执行结果:
std micro_second:[ 0], Performance micro_second:[ 0]
std micro_second:[ 0], Performance micro_second:[ 197]
std micro_second:[ 997], Performance micro_second:[ 1041]
std micro_second:[ 2996], Performance micro_second:[ 3631]
std micro_second:[ 3996], Performance micro_second:[ 3745]
std micro_second:[ 3996], Performance micro_second:[ 3804]
std micro_second:[ 3996], Performance micro_second:[ 3861]
std micro_second:[ 3996], Performance micro_second:[ 3952]
std micro_second:[ 3996], Performance micro_second:[ 4011]
std micro_second:[ 3996], Performance micro_second:[ 4068]
std micro_second:[ 3996], Performance micro_second:[ 4132]
std micro_second:[ 3996], Performance micro_second:[ 4196]
std micro_second:[ 3996], Performance micro_second:[ 4253]
std micro_second:[ 3996], Performance micro_second:[ 4312]
std micro_second:[ 3996], Performance micro_second:[ 4370]
std micro_second:[ 3996], Performance micro_second:[ 4423]
std micro_second:[ 3996], Performance micro_second:[ 4478]
std micro_second:[ 3996], Performance micro_second:[ 4535]
std micro_second:[ 3996], Performance micro_second:[ 4592]
std micro_second:[ 4994], Performance micro_second:[ 4759]
linux下执行结果:
std micro_second:[ 2], Performance micro_second:[ 0]
std micro_second:[ 39], Performance micro_second:[ 37]
std micro_second:[ 42], Performance micro_second:[ 40]
std micro_second:[ 44], Performance micro_second:[ 42]
std micro_second:[ 47], Performance micro_second:[ 45]
std micro_second:[ 49], Performance micro_second:[ 47]
std micro_second:[ 51], Performance micro_second:[ 50]
std micro_second:[ 54], Performance micro_second:[ 52]
std micro_second:[ 56], Performance micro_second:[ 55]
std micro_second:[ 59], Performance micro_second:[ 57]
std micro_second:[ 61], Performance micro_second:[ 59]
std micro_second:[ 63], Performance micro_second:[ 62]
std micro_second:[ 66], Performance micro_second:[ 64]
std micro_second:[ 68], Performance micro_second:[ 67]
std micro_second:[ 71], Performance micro_second:[ 69]
std micro_second:[ 73], Performance micro_second:[ 71]
std micro_second:[ 75], Performance micro_second:[ 74]
std micro_second:[ 78], Performance micro_second:[ 76]
std micro_second:[ 80], Performance micro_second:[ 78]
std micro_second:[ 83], Performance micro_second:[ 81]
对比执行结果可见:
在Windows下,标准库获取系统时间的精度实现是低于标准库函数定义的精度,大约在10毫秒左右更新一次。
在Linux下,则能精确到微秒级别,而且因为性能原因执行耗时远远低于windows。
而根据CPU主频计算时间在两个操作系统下都能给出较高的精度。