请问为什么在电脑上秦九韶算法快不了多少啊?
本帖最后由 sunnyrubik 于 2019-9-8 22:33 编辑不是都说秦九韶比普通的写法快几倍吗?
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
#define MAXK 1e8 //被测函数最大重复调用次数
double f1(int n,double a[],double x);
double f2(int n,double a[],double x);//秦九韶算法
clock_t start,stop;
double duration;
int main()
{
int i;
double a;
for(i=0;i<10;i++)
{
a=(double)i;
}
start = clock();
for(i = 0;i < MAXK;i++)
f1(9,a,1.1);
stop = clock();
duration = ((double)(stop-start))/CLK_TCK/MAXK;//计算函数单次运行时间
printf("ticks1 = %f\n",(double)(stop-start));
printf("duration = %6.2e\n",duration);
start = clock();
for(i = 0;i < MAXK;i++)
f2(9,a,1.1);
stop = clock();
duration = ((double)(stop-start))/CLK_TCK/MAXK;
printf("ticks2 = %f\n",(double)(stop-start));
printf("duration = %6.2e\n",duration);
return 0;
}
double f1(int n,double a[],double x)
{
int i;
double p = a;
for(i=1;i<=n;i++)
{
p += (a * pow(x,i));
}
return p;
}
double f2(int n,double a[],double x)
{
int i;
double p = a;
for(i=n;i>0;i--)
{
p = a + x*p;
}
return p;
}
本帖最后由 sunnyrubik 于 2019-9-8 17:31 编辑
已解决。。。。。。
页:
[1]