原型:BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

作用:该函数是操作系统的性能统计分辨率,也就是每秒钟统计多少次的意思,返回硬件支持的高精度计数器的频率。返回非零,硬件支持高精度计数器,返回零,硬件不支持,读取失败。

QueryPerformanceCounter 是系统性能统计计数器,表示统计了多少次,除以QueryPerformanceFrequency,得到系统运行时间(秒数)。

#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>

int main()
{
    LARGE_INTEGER t1, t2, tc;
    QueryPerformanceFrequency(&tc);
    QueryPerformanceCounter(&t1);
    my_fun(); // 测试该函数耗时
    QueryPerformanceCounter(&t2);
    double time = (double)(t2.QuadPart - t1.QuadPart) / (double)tc.QuadPart;
    cout << "full_time = " << time << endl;  //输出时间(单位:s)
}