如何知道一个c程序的运行时间
发布网友
发布时间:2022-03-04 07:44
我来回答
共5个回答
热心网友
时间:2022-03-04 09:14
楼主想法是没错,不过照现在的电脑,运行你的这程序,
运行时间永远是0,因为clock每秒18.2次中断,
只有象回答者: 521linux - 江湖少侠 六级 11-2 18:04
他那样费时点的才能有值
热心网友
时间:2022-03-04 10:32
c/c++中的计时函数是clock()。
所以,可以用clock函数来计算的运行一个循环、程序或者处理其它事件到底花了多少时间,具体参考代码如下:
#include “stdio.h”
#include “stdlib.h”
#include “time.h”
int main( void )
{
long i = 10000000l;
clock_t start, finish;
double ration;
/* 测量一个事件持续的时间*/
printf( "time to do %ld empty loops is ", i );
start = clock();
while( i-- ) ;
finish = clock();
ration = (double)(finish - start) / clocks_per_sec;
printf( "%f seconds\n", ration );
system("pause");
}
热心网友
时间:2022-03-04 12:06
#include “stdio.h”
#include “stdlib.h”
#include “time.h”
int main( void )
{
long i = 10000000L;
clock_t start, finish;
double ration;
/* 测量一个事件持续的时间*/
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- ) ;
finish = clock();
ration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%f seconds\n", ration );
system("pause");
}
热心网友
时间:2022-03-04 13:58
#include<stdio.h>
#include<time.h>
void main()
{
time_t start = clock();
int a,b,c,t;
scanf("%d,%d,%d",&a,&b,&c);
if(a>b)
{t=a;a=b;b=t;}
if(a>c)
{t=a;a=c;c=t;}
if(b>c)
{t=b;b=c;c=t;}
printf("small to big:%d,%d,%d\n",a,b,c);
time_t end = clock();
printf("用时: %d s\n", (end-start) / CLOCKS_PER_SEC);
}
热心网友
时间:2022-03-04 16:06
//---------------------------------------------------------------------------
#include<stdio.h>
#include<time.h>
void main()
{
int a,b,c,t;
clock_t te;
te=clock();
scanf("%d,%d,%d",&a,&b,&c);
if(a>b)
{t=a;a=b;b=t;}
if(a>c)
{t=a;a=c;c=t;}
if(b>c)
{t=b;b=c;c=t;}
printf("small to big:%d,%d,%d\n",a,b,c);
te=clock()-te;
printf("TIME:%lf\n",te/CLOCKS_PER_SEC);
}
//---------------------------------------------------------------------------