·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> php网站开发 >> 多项式计算的效率测试

多项式计算的效率测试

作者:佚名      php网站开发编辑:admin      更新时间:2022-07-23
多项式计算的效率测试

多项式计算调用库函数pow方法和秦九韶算法,我们来测算下他们的运行效率

计算函数f(x)=1+(Σxi/i)(i从1取到m);

用ctime时间函数来测试运行时间,带入x=0.9来计算

#include<iostream>#include<cmath>;#include<ctime>using namespace std;double Fn1(double x);double Fn2(double x);#define m 1000000000clock_t start, stop;int main(){double x;x = 0.9;start = clock();cout << Fn1(x) << endl;stop = clock();cout << double(stop - start) / CLK_TCK << endl;//-----------------------------------start = clock();cout << Fn2(x) << endl;stop = clock();cout << double(stop - start) / CLK_TCK << endl;return 0;}double Fn1(double x){int i;double f=1.0;for (i = 1; i <= m; i++)f += pow(x, i)/i;return f;}double Fn2(double x){int i;double f = 0.0;for (i = m; i >= 1; i--) /*秦九韶多项式算法*/f = f*x + 1.0 / i;return f*x + 1.0;}

运行时间见下表格

m10010001000010000010000001000000010000001000000000
Fn10.0010.0010.0030.0150.1571.61917.955191.608
Fn20000.0010.0050.0490.4724.706

从运行时间的结果可以看出来,秦九韶算法效率远远高于pow调用方法