#include <stdio.h> #include <time.h> #include <math.h> #define maxrepeat 1e7#define maxn 101 typedef double (*func) (int, double );void timecost (func myfunc , int n , double x);d ouble func1 (int n , double x);d Ouble func2 (int n , double x); Int main (int argc , const char *argv[]) {int n =maxn;double x = 1.1;printf ("fun1 Result:%lf\n", Func1 (n,x)); Timecost (func1 , n, x);p rintf ("Func2 Result:%lf\n", Func2 (n,x)); Timecost (func2, n &NBSP;,X); return 0;} void timecost (func myfunc , int n , double x) {Clock_t start , stop;double duration;start = clock (); Int i;for ( i = 0; i < maxrepeat ; i++) {myFunc (n,x);} stop = clock ();d uration = ((Double) (Stop-start))/clocks_per_sec;printf ("spents%f seconds \ n", duration);} DOUBLE&NBSP;FUNC1 (int n, double x) {double p =1;int i;for ( i =1 ;i <n;i++) {P += pow (x,i)//;} Return p;} DOUBLE&NBSP;FUNC2 (int n ,double x) {double p =1.0/(n-1); Int i =0;for (i = n-1 ;i> 0 ; i--) {if (i ==1) {p = 1+x*p;} else{p =1.0/(i-1) + x*p;}} Return p;}
Data structure learning note solving polynomial 2