
如何在C++程序中添加计时器判断两个函数的运行时间长短
4个回答
展开全部
#include <ctime>
#include <iostream>
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循环~
#include <iostream>
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循环~
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
获取机器时钟。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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.
#include <stdlib.h>
#include <time.h>
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.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <windows.h>
#include <stdio.h>
#include <time.h>
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;
}
#include <stdio.h>
#include <time.h>
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;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询