三次多项式曲线php,多项式计算的效率测试,多项式计算效率_PHP教程
多項(xiàng)式計(jì)算的效率測(cè)試,多項(xiàng)式計(jì)算效率
多項(xiàng)式計(jì)算調(diào)用庫(kù)函數(shù)pow方法和秦九韶算法,我們來(lái)測(cè)算下他們的運(yùn)行效率
計(jì)算函數(shù)f(x)=1+(Σxi/i)(i從1取到m);
用ctime時(shí)間函數(shù)來(lái)測(cè)試運(yùn)行時(shí)間,帶入x=0.9來(lái)計(jì)算
#include#include;
#includeusing 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--) /*秦九韶多項(xiàng)式算法*/
f = f*x + 1.0 / i;
return f*x + 1.0;
}
運(yùn)行時(shí)間見下表格
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
從運(yùn)行時(shí)間的結(jié)果可以看出來(lái),秦九韶算法效率遠(yuǎn)遠(yuǎn)高于pow調(diào)用方法
http://www.bkjia.com/PHPjc/1045060.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1045060.htmlTechArticle多項(xiàng)式計(jì)算的效率測(cè)試,多項(xiàng)式計(jì)算效率 多項(xiàng)式計(jì)算調(diào)用庫(kù)函數(shù)pow方法和秦九韶算法,我們來(lái)測(cè)算下他們的運(yùn)行效率 計(jì)算函數(shù)f(x)=1+(x i /i)...
總結(jié)
以上是生活随笔為你收集整理的三次多项式曲线php,多项式计算的效率测试,多项式计算效率_PHP教程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Docker中快速安装Mysql
- 下一篇: bash ./php-memcache.