windows有QueryPerformanceFrequency和QueryPerformanceCounter两个api,通过他们可以精确的计算机的运行时间。
(1)QueryPerformanceFrequency能获取计算机内精确计时器每秒的滴答数。
如果硬件不支持,则会返回0表示失败。
(2)QueryPerformanceCounter能获取计算机内精确计时器从开机起的总滴答数。
如果硬件不支持,则会返回0表示失败。
例子代码:
#include <windows.h> #include <ctime> #include <iostream> using namespace std; int main() { LARGE_INTEGER freq; cout << "ret " << QueryPerformanceFrequency(&freq) << endl; cout << freq.QuadPart << endl; LARGE_INTEGER counter; cout << "ret " << QueryPerformanceCounter(&counter) << endl; cout << counter.QuadPart << endl; cout << counter.QuadPart / freq.QuadPart << endl; cout << time(NULL) << endl; return 0; }