BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency)获取系统的计数器的频率
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount)获取计数器的值
(原文出处:
coder_xia的博客:http://blog.csdn.net/coder_xia/article/details/6566708)
然后用两次计数器的差除以Frequency就得到时间。
测试程序如下:
- #include <iostream>
- #include <windows.h>
- using namespace std;
- int main()
- {
- LARGE_INTEGER m_nFreq;
- LARGE_INTEGER m_nBeginTime;
- LARGE_INTEGER nEndTime;
- QueryPerformanceFrequency(&m_nFreq); // 获取时钟周期
- QueryPerformanceCounter(&m_nBeginTime); // 获取时钟计数
- Sleep(100);
- QueryPerformanceCounter(&nEndTime);
- cout << (double)(nEndTime.QuadPart-m_nBeginTime.QuadPart)*1000/m_nFreq.QuadPart << endl;
- }
需要注意的就是结果需要强制转换为double,不然会得到如下错误:<< is ambiguous