c++统计函数耗时

原型:BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

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

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

cpp 复制代码
#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)
}
相关推荐
saltymilk1 天前
C++ 模板参数推导问题小记(模板类的模板构造函数)
c++·模板元编程
感哥1 天前
C++ lambda 匿名函数
c++
沐怡旸1 天前
【底层机制】std::unique_ptr 解决的痛点?是什么?如何实现?怎么正确使用?
c++·面试
感哥1 天前
C++ 内存管理
c++
博笙困了2 天前
AcWing学习——双指针算法
c++·算法
感哥2 天前
C++ 指针和引用
c++
感哥2 天前
C++ 多态
c++
沐怡旸2 天前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River4163 天前
Javer 学 c++(十三):引用篇
c++·后端
感哥3 天前
C++ std::set
c++