我要发布
仪器网/ 仪器社区/ 计时器/ 如何在C++程序中添加计时器判断两个函数的运行时间长短

如何在C++程序中添加计时器判断两个函数的运行时间长短

守望1座空城    2010-12-03    计时器    浏览 308 次

精彩问答
eryrejk 发布日期:2017-10-07
#include
#include
using namespace std;

int main () {
clock_t start, finish; //typedef long clock_t;
double totaltime;
start = clock(); //clock():Current time of CPU

for (int i = 0; i < 1000000; i++)
{
}

finish=clock();
totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
cout<<"\nRuntime is: " << totaltime << "s" << endl;
return 0;
}

中间位置是你需要测试的函数,为简便我只写了一个for循环~
被采纳
冰旭冰旭冰 发布日期:2010-12-04
#include
#include
#include

typedef long ClockT;

ClockT start;
ClockT finish;
double dtime;

int main ()
{
//开始计时保存到start
start=clock();

//具体运算
Sleep(1900);

//结束计时保存到finish
finish=clock();

dtime=(double)(finish-start);
printf("精确时间: %lf\n",dtime);
printf("一般时间: %lf\n",dtime/CLOCKS_PER_SEC);
return 0;
}
全部评论
wszaizhj 发布日期:2010-12-04
#include
#include
#include

void sleep( clock_t wait );

void main( void )
{
long i = 600000L;
clock_t start, finish;
double duration;

/* Delay for a specified time. */
printf( "Delay for three seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" );

/* Measure the duration of an event. */
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds\n", duration );
}

/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}

If you have another problems,you may call me.
酒醉伊人__e 发布日期:2016-01-09
获取机器时钟。
最新主题
相关版块
我要评论
X您尚未登录
账号登录
X您尚未登录
手机动态密码登录
X您尚未登录
扫码登录
官方微信

仪器网微信服务号

扫码获取最新信息


仪器网官方订阅号

扫码获取最新信息

在线客服

咨询客服

在线客服
工作日:  9:00-18:00
联系客服 企业专属客服
电话客服:  400-822-6768
工作日:  9:00-18:00
订阅商机

仪采招微信公众号

采购信息一键获取海量商机轻松掌控