获取函数运行时间

函数性能测试 

//统一性能测试
#include <iostream>
#include<Windows.h>
using namespace std;
template<typename func>
int performance_test(func f){			//通过传入成员函数指针,进行调用,测试函数运行时间
	LARGE_INTEGER frec, start, end;		//WindowsAPI测试程序运行时间
	QueryPerformanceFrequency(&frec);
	QueryPerformanceCounter(&start);
	f();								//此处调用函数
	QueryPerformanceCounter(&end);
	return (end.QuadPart - start.QuadPart) * 1000000000 / frec.QuadPart;
}
void show() {		//假设这个是算法的函数,你需要保证函数列表为空,因为这样才能统一测试,如果不为空,那么请在算法函数外增加一层这样的函数
	Sleep(100);
}
void test() {
	Sleep(200);
}
int main(){
	cout << performance_test(show) << endl;		//传入函数对象,返回的是一个纳米级的运行时间
	cout << performance_test(test) << endl;		//传入函数对象,返回的是一个纳米级的运行时间
	return 0;
}

 截图

#include<Windows.h>
template<typename func>
int performance_test(func f){			//通过传入成员函数指针,进行调用,测试函数运行时间
	LARGE_INTEGER frec, start, end;		//WindowsAPI测试程序运行时间
	QueryPerformanceFrequency(&frec);
	QueryPerformanceCounter(&start);
	f();								//此处调用函数
	QueryPerformanceCounter(&end);
	return (end.QuadPart - start.QuadPart) * 1000000000 / frec.QuadPart;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值