Home > Backend Development > PHP Tutorial > Efficiency test of polynomial calculation, polynomial calculation efficiency_PHP tutorial

Efficiency test of polynomial calculation, polynomial calculation efficiency_PHP tutorial

WBOY
Release: 2016-07-13 09:44:55
Original
1058 people have browsed it

Efficiency test of polynomial calculation, polynomial calculation efficiency

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1045060.htmlTechArticleEfficiency test of polynomial calculation. Polynomial calculation calls the library function pow method and Qin Jiushao algorithm. Let’s measure it. Their operating efficiency calculation function f(x)=1 (x i /i)...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template