您的位置:首页 > 编程语言 > C语言/C++

【c++】检测一个函数运行的时间

2017-10-08 17:41 260 查看
函数运行需要时间,这个时间可以用如下方法进行测试



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

void test()
{
cout << "nihao" << endl;
cout << "nihao" << endl;
cout << "nihao" << endl;
cout << "nihao" << endl;
cout << "nihao" << endl;
cout << "nihao" << endl;
cout << "nihao" << endl;
}
DWORD TimeTest()
{
DWORD t;
t = GetTickCount();
test();
return GetTickCount() - t;
}
void FunTest()
{
DWORD T = TimeTest();
cout << T << endl;
}
int main()
{
FunTest();
system("pause");
return 0;
}
在此代码中我测试的是test函数运行的时间,并将时间在屏幕上进行输出,该程序精确度能达到一毫秒,最后输出的单位也是毫秒。



如图所示,该程序的·运行时间为15毫秒
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: