int main(){ double vals1[NMAX+1], vals2[NMAX+1]; integralGen(1,100, vals2); integral_recur(1, 100, vals1); printf("n Recursion gsl\n"); for(int j =1; j<NMAX+1; j++){ printf("%d %f %f\n",j,vals1[j], vals2[j]); } int count1 = 1000; double timea; double timea1; double tmin = 1.; double tmax = 2.; do { timer_start (); for (int i = 0; i <count1; i++){ integral_recur(1, 100, vals1); } timea = timer_stop (); timea1 = timea / count1; printf ("Recursion: %10.2f usec %10.6f sec %10d\n", timea1 * 1.e6, timea, count1); count1 = adjust_rep_count (count1, timea, tmin, tmax); } while ((timea >tmax) || (timea < tmin)); int count2 = 1000; double timeb; double timeb1; do { timer_start (); for (int i = 0; i <count2; i++){ integralGen(1,100, vals2); } timeb = timer_stop (); timeb1 = timeb / count2; printf ("gsl Integration: %10.2f usec %10.6f sec %10d\n", timeb1 * 1.e6, timeb, count2); count2 = adjust_rep_count (count2, timeb, tmin, tmax); } while ((timeb > tmax) || (timeb < tmin)); }
int main(void) { #define NMAX 100 //array size for storing the values of I_n int nminn = 0; //min value of n to be calulated for integrals int nmaxx = 100; //max value of n to be calculated for integrals double vals1[NMAX + 1], vals2[NMAX + 1]; integral_recur (nminn, nmaxx, vals1); integral_gen (nminn, nmaxx, vals2); // prints and compares the values of the different methods for(int i = 0,j = nminn; i <= nmaxx - nminn; i++,j++) { printf("%.18f\t%.18f\t%d\n", vals1[i], vals2[i], j); } int count = 10; double time; double time1; double tmin = 1; double tmax = 2; // times integral_recur and integral_gen per function call // adjusts the number of calls such that there are enough calls to get a good // average time per function call printf("integral_recur\n"); do { timer_start (); for (int k = 0; k <= count; k++) { integral_recur (nminn, nmaxx, vals1); } time = timer_stop (); time1 = time / count; printf (" %10.2f usec %10.6f sec %10d\n", time1 * 1.e6, time, count); /* * adjust count such that cpu time is between * tmin and tmax */ count = adjust_rep_count (count, time, tmin, tmax); } while ((time > tmax) || (time < tmin)); printf("integral_gen\n"); count = 10; do { timer_start (); for (int k = 0; k <= count; k++) { integral_gen (nminn, nmaxx, vals2); } time = timer_stop (); time1 = time / count; printf (" %10.2f usec %10.6f sec %10d\n", time1 * 1.e6, time, count); /* * adjust count such that cpu time is between * tmin and tmax */ count = adjust_rep_count (count, time, tmin, tmax); } while ((time > tmax) || (time < tmin)); return 0; }
int main(void) { /* Declaration and initialization */ int i; double vals1[NMAX+1], vals2[NMAX+1]; /*Uses functions declared in "funs.c" to compute I(n) using the recurrence and the generic integrator methods, and stores the results in their respective arrays.*/ integral_recur(0, 100, vals1); integral_gen(0, 100, vals2); /*Prints "n" and the result I(n) for each method above. The absolute error multiplied by a factor of 1.e9 is reported in the last column.*/ printf("n Recurrence Generic Scaled Abs Err\n"); for(i = 0; i <=NMAX; i++) { printf("%d %.8f %.8f %f\n", i, vals1[i], vals2[i], (fabs(vals1[i]-vals2[i]))*1.e9); } /* Define the intial number of counts, tmin, tmax*/ double time, trr, tgsl; int tmin = 1; int tmax = 2; int count = 1000; /* Computes the time per function call of the recurrence integration method. Calls the function integral_recur() "count" times and adjusts count until the total time is between 1 and 2 seconds.*/ printf("\n Timing for Recurrence Relation:\n"); do { timer_start (); for(i = 0; i < count; i++) { integral_recur(0, 100, vals1); } time = timer_stop (); trr = time /count; printf (" %10.2f usec %10.6f sec %10d\n",trr * 1.e6, time, count); count = adjust_rep_count (count, time, tmin, tmax); } while ((time >tmax) || (time <tmin)); /* Computes the time per function call of the GSL integrator method. Calls the function integral_gen() "count" times and adjusts count until the total time is between 1 and 2 seconds. */ printf("\n Timing for GSL Integrator:\n"); do { timer_start (); for(i = 0; i < count; i++) { integral_gen(0, 100, vals1); } time = timer_stop (); tgsl = time /count; printf (" %10.2f usec %10.6f sec %10d\n",tgsl * 1.e6, time, count); count = adjust_rep_count (count, time, tmin, tmax); } while ((time >tmax) || (time <tmin)); /*Prints time per function call for leibniz and bbp series, and then takes the ratio between the times to compare the relative speed*/ printf("\nTime per function call for Recur: %10.6f usec", trr*1.e6); printf("\nTime per function call for GSL: %10.6f usec", tgsl*1.e6); int ratio = round(tgsl/trr); printf("\nRatio of function times tgsl/trr: %d\n",ratio); }