函数运行需要时间,这个时间可以用如下方法进行测试
:
#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毫秒