diff options
author | Matt Turner <mattst88@gmail.com> | 2008-11-14 00:01:53 +0000 |
---|---|---|
committer | Matt Turner <mattst88@gmail.com> | 2008-11-14 00:01:53 +0000 |
commit | 7af55db3dc3e0287daa1085e186dcc5dffe1083a (patch) | |
tree | ae00ea8a9b2608d1fa18c659be4983c4dad593e6 /stringsort.c | |
parent | 682804e40ceb3c611923e2fac3c1d04626faab00 (diff) |
-- 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
Diffstat (limited to 'stringsort.c')
-rw-r--r-- | stringsort.c | 246 |
1 files changed, 90 insertions, 156 deletions
diff --git a/stringsort.c b/stringsort.c index d3417a2..b302f69 100644 --- a/stringsort.c +++ b/stringsort.c @@ -4,6 +4,7 @@ #include <string.h> #include <math.h> #include <limits.h> +#include <time.h> #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)<strsortstruct->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<numarrays;i++) -{ StrHeapSort(tempobase,tempsbase,nstrings,0L,nstrings-1); - tempobase+=nstrings; /* Advance base pointers */ - tempsbase+=arraysize+100; -} + unsigned long *optrarray; /* Offset pointer array */ + clock_t start, stop; + 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 = clock(); + + for(i = 0; i < numarrays; i++) { + StrHeapSort(tempobase, tempsbase, nstrings, 0L, nstrings - 1); + tempobase += nstrings; /* Advance base pointers */ + tempsbase += arraysize+100; + } -/* -** Record elapsed time -*/ -elapsed=StopStopwatch(elapsed); + stop = clock(); -#ifdef DEBUG -{ - unsigned long i; - for(i=0;i<nstrings-1;i++) - { /* - ** Compare strings to check for proper - ** sort. - */ - if(str_is_less(optrarray,arraybase,nstrings,i+1,i)) - { printf("Sort Error\n"); - stringsort_status=1; - break; - } - } -} -#endif - -/* -** Release the offset pointer array built by -** LoadStringArray() -*/ -free(optrarray); + free(optrarray); -/* -** Return elapsed ticks. -*/ -return(elapsed); + return stop - start; } /******************** |