Polynomial calculation calls the library function pow method and Qin Jiushao algorithm, let’s measure their operating efficiency
Calculate function f(x)=1 (Σxi/i) (i is taken from 1 to m);
Use the ctime time function to test the running time, and bring in x=0.9 to calculate
#include
#include
#include
using namespace std;
double Fn1(double x);
double Fn2(double x );
#define m 1000000000
clock_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--) /*Qin Jiushao polynomial algorithm*/
f = f*x 1.0 / i;
return f*x 1.0;
}
See the table below for running time
m | 100 | 1000 | 10000 | 100000 | 1000000 | 10000000 | 1000000 | 1000000000 |
Fn1 | 0.001 | 0.001 | 0.003 | 0.015 | 0.157 | 1.619 | 17.955 | 191.608 |
Fn2 | 0 | 0 | 0 | 0.001 | 0.005 | 0.049 | 0.472 | 4.706 |
It can be seen from the running time results that the efficiency of Qin Jiushao's algorithm is much higher than that of the pow calling method