失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 计算程序/函数运行时间

计算程序/函数运行时间

时间:2019-03-25 11:36:23

相关推荐

计算程序/函数运行时间

#include<math.h>

using namespace std;

clock_t start,stop;/* clock_t是clock()函数返回的变量类型*/

double duration;/* 记录被测函数运行时间,以秒为单位*/

double f1(double a[],int n,double x);

double f2(double a[],int n,double x);

int mian()

{

double a[1000],x;

int n;

while(scanf("%d%lf",&n,&x)!=EOF )

{

int j;

for (j=0;j<=n-1;j++) { scanf("%lf",&a[j]); }

/* 不在测试范围内的准备工作写在clock()调用之前*/

start = clock();/* 开始计时*/

f1(a,n,x);/* 把被测函数加在这里*/

stop = clock();/* 停止计时*/

duration = ((double)(stop - start))/CLK_TCK;/* 计算运行时间*/

printf("%lf\n",duration);

start= clock();/* 开始计时*/

f2(a,n,x);/* 把被测函数加在这里*/

stop= clock();/* 停止计时*/

duration= ((double)(stop - start))/CLK_TCK;/* 计算运行时间*/

printf("%lf",duration);

}

return 0;

}

double f1(double a[],int n,double x)

{

int i;

double p=a[0];

for(i=1;i<=n-1;i++)

{

p=p+a[i]*pow(x,i);

}

return p;

}

double f2(double a[],int n,double x)

{ int i;

double p = a[n];

for ( i=n; i>0; i-- )

p = a[i-1] + x*p;

return p;

}

如果觉得《计算程序/函数运行时间》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。