#include <time.h>
#include <stdio.h>
//获取当前时间
double getCurrentTimestamp() {
#ifdef _WIN32 // Windows
// Use the high-resolution performance counter.
static LARGE_INTEGER ticks_per_second = {};
if(ticks_per_second.QuadPart == 0) {
// First call - get the frequency.
QueryPerformanceFrequency(&ticks_per_second);
}
LARGE_INTEGER counter;
QueryPerformanceCounter(&counter);
double seconds = double(counter.QuadPart) / double(ticks_per_second.QuadPart);
return seconds;
#else // Linux
timespec a;
clock_gettime(CLOCK_MONOTONIC, &a);
return (double(a.tv_nsec) * 1.0e-9) + double(a.tv_sec);
#endif
}
main()
{
//第一次时间
const double start_time = getCurrentTimestamp();
//插入运行的代码
//第二次时间
const double end_time = getCurrentTimestamp();
//统计时间差
const double total_time = end_time - start_time;
printf("\nTotal Time: %0.3f ms\n", total_time * 1e3);
}