c++或c语言怎么测得调用一个函数或者一个递归的运行时间(因为运行时间短,所以要精确到毫秒)
1条回答 默认 最新
追逐时代的脚步 2020-12-17 13:14关注#include <iostream>
#include <windows.h>static LARGE_INTEGER cpuFreq;
static LARGE_INTEGER startTime;
static LARGE_INTEGER endTime;
static double run_time = 0.0;using namespace std;
int func()
{
return 1+1;
}int main()
{QueryPerformanceFrequency(&cpuFreq); //获取系统时钟的频率
QueryPerformanceCounter(&startTime);
func();
QueryPerformanceCounter(&endTime);
run_time = (((endTime.QuadPart - startTime.QuadPart) * 1000.0) / cpuFreq.QuadPart);
cout << "run_time: "<< run_time <<"ms" << endl;while(1);
}本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 2无用