From 86416e086b264b7709d7e9b3810dda5536146a8a Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Sun, 16 Nov 2008 02:37:26 +0000 Subject: Clean up cleanbench.c nicely git-svn-id: svn://mattst88.com/svn/cleanbench/trunk@62 0d43b9a7-5ab2-4d7b-af9d-f64450cef757 --- cleanbench.c | 129 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 64 insertions(+), 65 deletions(-) diff --git a/cleanbench.c b/cleanbench.c index c5245f2..f2b8fd5 100644 --- a/cleanbench.c +++ b/cleanbench.c @@ -21,10 +21,10 @@ double DoHuffman(void); double DoNNET(void); double DoLU(void); -static int bench_with_confidence(int benchmark, double* average, double* std_dev, int* runs); -static int calc_confidence(double scores[], int num_scores, double *c_half_interval,double* average, double* std_dev); +static bool bench_with_confidence(int benchmark, double* average, double* std_dev, int* runs); +static bool calc_confidence(double scores[], int runs, double *c_half_interval,double* average, double* std_dev); -#define NUMTESTS 10 +#define NUM_TESTS 10 enum { NUMSORT, @@ -104,7 +104,7 @@ main(int argc, char *argv[]) " : : Pentium 90 : AMD K6/233\n" "--------------------:------------------:-------------:------------"); - for (; benchmark < NUMTESTS; benchmark++) { + for (; benchmark < NUM_TESTS; benchmark++) { printf("%s :", benchmark_name[benchmark]); if (!bench_with_confidence(benchmark, &average, &std_dev, &runs)) { @@ -160,10 +160,10 @@ main(int argc, char *argv[]) ** Return true; false on failure. Returns average ** and standard deviation through argument list if successful. */ -static int +static bool bench_with_confidence(int benchmark, double* average, double* std_dev, int* runs) { - double (*funcpointer[])(void) = { + double (*Do[])(void) = { DoNumSort, DoEmFloat, DoIDEA, @@ -176,7 +176,7 @@ bench_with_confidence(int benchmark, double* average, double* std_dev, int* runs DoLU }; - double myscores[30]; /* Need at least 5 scores, use at most 30 */ + double score[30]; /* Need at least 5 scores, use at most 30 */ double c_half_interval; /* Confidence half interval */ int i; /* Index */ @@ -184,7 +184,7 @@ bench_with_confidence(int benchmark, double* average, double* std_dev, int* runs ** Get first 5 scores. Then begin confidence testing. */ for (i = 0; i < 5; i++) { - myscores[i] = (*funcpointer[benchmark])(); + score[i] = (*Do[benchmark])(); } *runs = 5; /* Show 5 attempts */ @@ -198,12 +198,10 @@ bench_with_confidence(int benchmark, double* average, double* std_dev, int* runs */ while(1) { /* Calculate confidence. Should always return true */ - if (0!=calc_confidence(myscores, - *runs, - &c_half_interval, - average, - std_dev)) return false; - + if (!calc_confidence(score, *runs, &c_half_interval, average, std_dev)) { + return false; + } + /* ** Is the length of the half interval 5% or less of average? ** If so, we can go home. Otherwise, we have to continue. @@ -212,69 +210,70 @@ bench_with_confidence(int benchmark, double* average, double* std_dev, int* runs break; } - /* We now simply add a new test run and hope that the runs - finally stabilize, Uwe F. Mayer */ - if(*runs == 30) return false; - myscores[*runs] = (*funcpointer[benchmark])(); + if (*runs == 30) { + return false; + } + + score[*runs] = (*Do[benchmark])(); *runs += 1; } return true; } -/******************** + /******************** ** calc_confidence ** ********************* -** Given a set of numtries scores, calculate the confidence -** half-interval. We'll also return the sample average and sample -** standard deviation. +** Given a set of scores, calculate the confidence +** half-interval. We'll also return the sample average +** and sample standard deviation. ** NOTE: This routines presumes a confidence of 95% and ** a confidence coefficient of .95 -** returns 0 if there is an error, otherwise -1 +** returns false if there is an error, otherwise true */ -static int calc_confidence(double scores[], /* Array of scores */ - int num_scores, /* number of scores in array */ - double *c_half_interval, /* Confidence half-int */ - double *average, /* Standard average */ - double *std_dev) /* Sample stand dev */ +static bool calc_confidence(double scores[], int runs, double* c_half_interval, double* average, double* std_dev) { -/* Here is a list of the student-t distribution up to 29 degrees of - freedom. The value at 0 is bogus, as there is no value for zero - degrees of freedom. */ -double student_t[30]={0.0 , 12.706 , 4.303 , 3.182 , 2.776 , 2.571 , - 2.447 , 2.365 , 2.306 , 2.262 , 2.228 , - 2.201 , 2.179 , 2.160 , 2.145 , 2.131 , - 2.120 , 2.110 , 2.101 , 2.093 , 2.086 , - 2.080 , 2.074 , 2.069 , 2.064 , 2.060 , - 2.056 , 2.052 , 2.048 , 2.045 }; -int i; /* Index */ -if ((num_scores<2) || (num_scores>30)) { - puts("Internal error: calc_confidence called with an illegal number of scores"); - return true; -} -/* -** First calculate average. -*/ -*average=(double)0.0; -for(i=0;i30)) { + fputs("Internal error: calc_confidence called with an illegal number of scores", stderr); + return false; + } + + /* First, calculate average.*/ + *average = 0.0; + for (i = 0; i < runs; i++) { + *average += scores[i]; + } + *average /= (double)runs; + + /* Get standard deviation */ + *std_dev = 0.0; + for (i = 0; i < runs; i++) { + *std_dev += (scores[i] - (*average)) * (scores[i] - (*average)); + } + *std_dev /= (double)(runs - 1); + *std_dev = sqrt(*std_dev); + + /* Now calculate the length of the confidence half-interval. For a + ** confidence level of 95% our confidence coefficient gives us a + ** multiplying factor of the upper .025 quartile of a t distribution + ** with runs-1 degrees of freedom, and dividing by sqrt(number of + ** observations). See any introduction to statistics. + */ + *c_half_interval = t_distribution[runs - 1] * (*std_dev) / sqrt((double)runs); + + return true; } -- cgit v1.2.3