分类: C/C++
2009-10-12 14:40:59
|
在调试中,经常需要计算某一段代码的执行时间,下面给出两种常用的方式:
第一种:使用GetTickCount函数 #include<iostream> #include<windows.h>![]() int main()![]() ![]() { DWORD start_time=GetTickCount();![]() { //此处为被测试代码 } DWORD end_time=GetTickCount(); cout<<"The run time is:"<<(end_time-start_time)<<"ms!"<<endl;//输出运行时间 return 0; }第二种:使用clock()函数 #include<iostream> #include<time.h>![]() int main()![]() ![]() { clock_t start_time=clock();![]() ![]() { //被测试代码 }![]() clock_t end_time=clock(); cout<< "Running time is: "<<static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC*1000<<"ms"<<endl;//输出运行时间 return 0; }由上面分析可知,用clock()函数计算运行时间,表示范围一定大于GetTickCount()函数,所以,建议使用clock()函数。 |
chinaunix网友2009-10-25 22:00:34
这样作误差很大的,windows上存在一种方式是直接获取当前代码断的CPU占用时间,很早以前在作ACM题目时看到过,不知linux下有没有。
chinaunix网友2009-10-18 18:11:44
现在3G网络,都不怎么好,也就中移动拔尖点。现在中国移动还大力推广OPhone 手机操作系统和手机,先进的 平台和人性化的操作界面,将得到广大手机用户的追捧,为手机软件市场带来巨大商机。欢迎程序员访问。 http://www.ophonesdn.com/