From 7af55db3dc3e0287daa1085e186dcc5dffe1083a Mon Sep 17 00:00:00 2001 From: Matt Turner Date: Fri, 14 Nov 2008 00:01:53 +0000 Subject: -- Remove useless stop watch functions from numsort.c and stringsort.c git-svn-id: svn://mattst88.com/svn/cleanbench/trunk@22 0d43b9a7-5ab2-4d7b-af9d-f64450cef757 --- numsort.c | 63 +++++---------- stringsort.c | 246 ++++++++++++++++++++++------------------------------------- 2 files changed, 111 insertions(+), 198 deletions(-) diff --git a/numsort.c b/numsort.c index 41928f8..f07c995 100644 --- a/numsort.c +++ b/numsort.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "nmglobal.h" #include "nbench1.h" @@ -15,7 +16,7 @@ ** This test implements a heapsort algorithm, performed on an ** array of longs. */ -static unsigned long DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int numarrays); +static clock_t DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int numarrays); static void LoadNumArrayWithRand(long *array, unsigned long arraysize, unsigned int numarrays); static inline void NumHeapSort(long *array, unsigned long bottom, unsigned long top); static inline void NumSift(long *array, unsigned long min, unsigned long max); @@ -29,19 +30,17 @@ static inline void NumSift(long *array, unsigned long min, unsigned long max); void DoNumSort (void) { - /* Error context string pointer */ - const char *context = "CPU:Numeric Sort"; - /* Local pointer to global struct */ - SortStruct * numsortstruct = &global_numsortstruct; - - long accumtime; /* Accumulated time */ - double iterations; /* Iteration counter */ - long *arraybase = NULL; /* Base pointers of array */ + const char* context = "CPU:Numeric Sort"; + SortStruct* numsortstruct = &global_numsortstruct; + clock_t total_time = 0; + int iterations = 0; + long* arraybase = NULL; /* ** See if we need to do self adjustment code. */ - if (numsortstruct->adjust == 0) { + if (numsortstruct->adjust == FALSE) { + numsortstruct->adjust = TRUE; /* ** Self-adjustment code. The system begins by sorting 1 ** array. If it does that in no time, then two arrays @@ -49,6 +48,7 @@ DoNumSort (void) ** enough arrays are built to handle the tolerance. */ numsortstruct->numarrays = 1; + while (1) { arraybase = realloc(arraybase, numsortstruct->numarrays * numsortstruct->arraysize * sizeof(long)); if (!arraybase) { @@ -83,25 +83,15 @@ DoNumSort (void) ** All's well if we get here. Repeatedly perform sorts until the ** accumulated elapsed time is greater than # of seconds requested. */ - accumtime = 0L; - iterations = 0.0; do { - accumtime += DoNumSortIteration(arraybase, numsortstruct->arraysize, numsortstruct->numarrays); - iterations += 1.0; - } while ( TicksToSecs(accumtime) < numsortstruct->request_secs ); + total_time += DoNumSortIteration(arraybase, numsortstruct->arraysize, numsortstruct->numarrays); + ++iterations; + } while (total_time / CLOCKS_PER_SEC < numsortstruct->request_secs); - /* - ** Clean up, calculate results, and go home. Be sure to - ** show that we don't have to rerun adjustment code. - */ free(arraybase); - numsortstruct->sortspersec = iterations * (double)numsortstruct->numarrays / TicksToFracSecs(accumtime); - - if (numsortstruct->adjust == 0) { - numsortstruct->adjust = 1; - } + numsortstruct->sortspersec = (double)(iterations * numsortstruct->numarrays * CLOCKS_PER_SEC) / (double)total_time; } /*********************** @@ -111,10 +101,10 @@ DoNumSort (void) ** sort benchmark. It returns the number of ticks ** elapsed for the iteration. */ -static unsigned long +static clock_t DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int numarrays) { - unsigned long elapsed; /* Elapsed ticks */ + clock_t start, stop; unsigned long i; /* @@ -122,24 +112,15 @@ DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int numarr */ LoadNumArrayWithRand(arraybase, arraysize, numarrays); - /* - ** Start the stopwatch - */ - elapsed = StartStopwatch(); + start = clock(); - /* - ** Execute a heap of heapsorts - */ for (i = 0; i < numarrays; i++) { - NumHeapSort(arraybase + i * arraysize, 0L, arraysize - 1L); + NumHeapSort(&arraybase[i], 0L, arraysize - 1L); } - /* - ** Get elapsed time - */ - elapsed = StopStopwatch(elapsed); + stop = clock(); - return(elapsed); + return stop - start; } /************************* @@ -148,9 +129,7 @@ DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int numarr ** Load up an array with random longs. */ static void -LoadNumArrayWithRand(long *array, /* Pointer to arrays */ - unsigned long arraysize, - unsigned int numarrays) /* # of elements in array */ +LoadNumArrayWithRand(long *array, unsigned long arraysize, unsigned int numarrays) { long i; /* Used for index */ long *darray; /* Destination array pointer */ diff --git a/stringsort.c b/stringsort.c index d3417a2..b302f69 100644 --- a/stringsort.c +++ b/stringsort.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "nmglobal.h" #include "nbench1.h" @@ -13,7 +14,7 @@ ** STRING HEAPSORT ** ********************/ -static unsigned long DoStringSortIteration(unsigned char *arraybase, +static clock_t DoStringSortIteration(unsigned char *arraybase, unsigned int numarrays, unsigned long arraysize); static unsigned long *LoadStringArray(unsigned char *strarray, @@ -51,99 +52,65 @@ static void strsift(unsigned long *optrarray, */ void DoStringSort(void) { - -SortStruct *strsortstruct; /* Local for sort structure */ -unsigned char *arraybase = NULL; /* Base pointer of char array */ -long accumtime; /* Accumulated time */ -double iterations; /* # of iterations */ -char *context; /* Error context string pointer */ -int systemerror; /* For holding error code */ - -/* -** Link to global structure -*/ -strsortstruct=&global_strsortstruct; - -/* -** Set the error context -*/ -context="CPU:String Sort"; - -/* -** See if we have to perform self-adjustment code -*/ -if(strsortstruct->adjust==0) -{ - /* - ** Initialize the number of arrays. - */ - strsortstruct->numarrays=1; - while(1) - { - /* - ** Allocate space for array. We'll add an extra 100 - ** bytes to protect memory as strings move around - ** (this can happen during string adjustment) - */ - arraybase = realloc(arraybase, (strsortstruct->arraysize + 100) * strsortstruct->numarrays); - if (!arraybase) { - fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); - exit(1); - } - - /* - ** Do an iteration of the string sort. If the - ** elapsed time is less than or equal to the permitted - ** minimum, then de-allocate the array, reallocate a - ** an additional array, and try again. - */ - if(DoStringSortIteration(arraybase, - strsortstruct->numarrays, - strsortstruct->arraysize)>global_min_ticks) - break; /* We're ok...exit */ - - strsortstruct->numarrays+=1; - } -} -else -{ - /* - ** We don't have to perform self adjustment code. - ** Simply allocate the space for the array. - */ - arraybase = malloc((strsortstruct->arraysize + 100) * strsortstruct->numarrays); - if (!arraybase) { - fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); - exit(1); + const char* context = "CPU:String Sort"; + SortStruct* strsortstruct = &global_strsortstruct; + unsigned char* arraybase = NULL; + clock_t total_time = 0; + int iterations = 0; + + /* + ** See if we have to perform self-adjustment code + */ + if (strsortstruct->adjust == FALSE) { + strsortstruct->adjust = TRUE; + /* + ** Initialize the number of arrays. + */ + strsortstruct->numarrays = 1; + while (1) { + /* + ** Allocate space for array. We'll add an extra 100 + ** bytes to protect memory as strings move around + ** (this can happen during string adjustment) + */ + arraybase = realloc(arraybase, (strsortstruct->arraysize + 100) * strsortstruct->numarrays); + if (!arraybase) { + fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); + exit(1); + } + + /* + ** Do an iteration of the string sort. If the + ** elapsed time is less than or equal to the permitted + ** minimum, then de-allocate the array, reallocate a + ** an additional array, and try again. + */ + if (DoStringSortIteration(arraybase, strsortstruct->numarrays, strsortstruct->arraysize) > global_min_ticks) { + break; + } + + ++strsortstruct->numarrays; + } + } else { + /* + ** We don't have to perform self adjustment code. + ** Simply allocate the space for the array. + */ + arraybase = malloc((strsortstruct->arraysize + 100) * strsortstruct->numarrays); + if (!arraybase) { + fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context); + exit(1); + } } -} -/* -** All's well if we get here. Repeatedly perform sorts until the -** accumulated elapsed time is greater than # of seconds requested. -*/ -accumtime=0L; -iterations=(double)0.0; -do { - accumtime+=DoStringSortIteration(arraybase, - strsortstruct->numarrays, - strsortstruct->arraysize); - iterations+=(double)strsortstruct->numarrays; -} while(TicksToSecs(accumtime)request_secs); + do { + total_time += DoStringSortIteration(arraybase, strsortstruct->numarrays, strsortstruct->arraysize); + iterations += strsortstruct->numarrays; + } while (total_time / CLOCKS_PER_SEC < strsortstruct->request_secs); -/* -** Clean up, calculate results, and go home. -** Set flag to show we don't need to rerun adjustment code. -*/ -free(arraybase); -strsortstruct->sortspersec=iterations / (double)TicksToFracSecs(accumtime); -if(strsortstruct->adjust==0) - strsortstruct->adjust=1; -#ifdef DEBUG -if (stringsort_status==0) printf("String sort: OK\n"); -stringsort_status=0; -#endif -return; + free(arraybase); + + strsortstruct->sortspersec = (double)(iterations * CLOCKS_PER_SEC) / (double)total_time; } /************************** @@ -154,75 +121,42 @@ return; ** Note that this routine also builds the offset pointer ** array. */ -static unsigned long DoStringSortIteration(unsigned char *arraybase, - unsigned int numarrays,unsigned long arraysize) +static clock_t +DoStringSortIteration(unsigned char *arraybase, unsigned int numarrays,unsigned long arraysize) { -unsigned long *optrarray; /* Offset pointer array */ -unsigned long elapsed; /* Elapsed ticks */ -unsigned long nstrings; /* # of strings in array */ -int syserror; /* System error code */ -unsigned int i; /* Index */ -unsigned long *tempobase; /* Temporary offset pointer base */ -unsigned char *tempsbase; /* Temporary string base pointer */ - -/* -** Load up the array(s) with random numbers -*/ -optrarray=LoadStringArray(arraybase,numarrays,&nstrings,arraysize); - -/* -** Set temp base pointers...they will be modified as the -** benchmark proceeds. -*/ -tempobase=optrarray; -tempsbase=arraybase; - -/* -** Start the stopwatch -*/ -elapsed=StartStopwatch(); - -/* -** Execute heapsorts -*/ -for(i=0;i