函数运行需要时间,这个时间可以用如下方法进行测试
:
#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毫秒
本文介绍了一种使用C++来测量函数运行时间的方法,并通过一个示例程序展示了如何利用GetTickCount()函数来获取毫秒级的时间精度,最终输出了test函数的运行时间为15毫秒。

1195

被折叠的 条评论
为什么被折叠?



