C++获取操作的精确时间


C++ #时间2012-11-09 10:51

使用下面两个函数

BOOL QueryPerformanceFrequency(LARGE_INTEGER * lpFrequency);

BOOL QueryPerformanceCounter(LARGE_INTEGER * lpCount);

第一个函数:返回硬件支持的高精度计数器的频率。

第二个函数:得到高精度计时器的值


其使用过程也是相当的简单:直接看源代码:

#include < iostream > 
#include < windows.h > using namespace std;


int main() {
    LARGE_INTEGER Frequency; //计数器频率 http://yige.org
    LARGE_INTEGER start_PerformanceCount; //起始计数器
    LARGE_INTEGER end_PerformanceCount; //结束计数器
    double run_time; //运行时间
    QueryPerformanceFrequency( & Frequency);


    for (int i = 0; i < 10; ++i) {


        QueryPerformanceCounter( & start_PerformanceCount);


        //运行测试的代码
        Sleep(10);


        QueryPerformanceCounter( & end_PerformanceCount);
        run_time = (end_PerformanceCount.QuadPart - start_PerformanceCount.QuadPart) / (double) Frequency.QuadPart;
        cout << run_time << endl;
    }


    return 0;
}


相关文章

粤ICP备11097351号-1