Home >Backend Development >PHP Tutorial >Efficiency Test for Polynomial Calculations

Efficiency Test for Polynomial Calculations

WBOY
WBOYOriginal
2016-07-30 13:32:011012browse

Polynomial calculation calls the library function pow method and Qin Jiutao algorithm. Let’s measure their operating efficiency

Calculation 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 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 Jiuchao polynomial algorithm*/
f = f*x + 1.0 / i;
return f*x + 1.0;
}

See the table below for the 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.0 49 0.472 4.706

It can be seen from the running time results , Qin Jiushao algorithm is much more efficient than the pow calling method

The above has introduced the efficiency test of polynomial calculations, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn