The efficiency test of polynomial calculation and the efficiency of polynomial calculation _php tutorial

Source: Internet
Author: User
Tags cmath

The efficiency test of polynomial calculation and the efficiency of polynomial calculation


Polynomial calculation call library function Pow method and Qin Jiushao algorithm, we can measure the efficiency of their operation

Calculation function f (x) =1+ (σxi/i) (I from 1 to M);

Use the CTime time function to test the run time and bring it into the 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;
}

Run time See table below

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 results of the running time that the Qin Jiushao algorithm is much more efficient than the POW call method

http://www.bkjia.com/PHPjc/1045060.html www.bkjia.com true http://www.bkjia.com/PHPjc/1045060.html techarticle the efficiency test of polynomial calculation, polynomial calculation efficiency polynomial calculation call library function Pow method and Qin Jiushao algorithm, we calculate the efficiency of their operation F (x) =1+ (x i/i) ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.