summaryrefslogtreecommitdiff
path: root/numsort.c
diff options
context:
space:
mode:
Diffstat (limited to 'numsort.c')
-rw-r--r--numsort.c44
1 files changed, 24 insertions, 20 deletions
diff --git a/numsort.c b/numsort.c
index 690d5a6..b9765cf 100644
--- a/numsort.c
+++ b/numsort.c
@@ -16,8 +16,15 @@
** This test implements a heapsort algorithm, performed on an
** array of longs.
*/
-static clock_t DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int num_arrays);
-static void LoadNumArrayWithRand(long *array, unsigned long arraysize, unsigned int num_arrays);
+
+/*
+** The following constant NUMARRAYSIZE determines the
+** default # of elements in each numeric array
+*/
+#define ARRAY_SIZE 8111L
+
+static clock_t DoNumSortIteration(long *array, unsigned int num_arrays);
+static void LoadNumArrayWithRand(long *array, unsigned int num_arrays);
static inline void NumHeapSort(long *array, unsigned long bottom, unsigned long top);
static inline void NumSift(long *array, unsigned long min, unsigned long max);
@@ -32,7 +39,7 @@ DoNumSort (void)
{
const char* context = "CPU:Numeric Sort";
SortStruct* numsortstruct = &global_numsortstruct;
- long* arraybase = NULL;
+ long* array = NULL;
clock_t total_time = 0;
int iterations = 0;
static int num_arrays = 1;
@@ -48,8 +55,8 @@ DoNumSort (void)
*/
while (1) {
- arraybase = realloc(arraybase, num_arrays * numsortstruct->arraysize * sizeof(long));
- if (!arraybase) {
+ array = realloc(array, num_arrays * ARRAY_SIZE * sizeof(long));
+ if (!array) {
fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
exit(1);
}
@@ -60,18 +67,15 @@ DoNumSort (void)
** minimum, then allocate for more arrays and
** try again.
*/
- if (DoNumSortIteration(arraybase, numsortstruct->arraysize, num_arrays) > MINIMUM_TICKS) {
+ if (DoNumSortIteration(array, num_arrays) > MINIMUM_TICKS) {
break;
}
- if (num_arrays > NUMNUMARRAYS) {
- puts("CPU:NSORT -- NUMNUMARRAYS hit.");
- }
++num_arrays;
}
} else {
- arraybase = malloc(num_arrays * numsortstruct->arraysize * sizeof(long));
- if (!arraybase) {
+ array = malloc(num_arrays * ARRAY_SIZE * sizeof(long));
+ if (!array) {
fprintf(stderr, "Error in %s, could not allocate memory. Exitting...\n", context);
exit(1);
}
@@ -83,11 +87,11 @@ DoNumSort (void)
*/
do {
- total_time += DoNumSortIteration(arraybase, numsortstruct->arraysize, num_arrays);
+ total_time += DoNumSortIteration(array, num_arrays);
++iterations;
} while (total_time < MINIMUM_SECONDS * CLOCKS_PER_SEC);
- free(arraybase);
+ free(array);
numsortstruct->results = (double)(iterations * num_arrays * CLOCKS_PER_SEC) / (double)total_time;
}
@@ -100,7 +104,7 @@ DoNumSort (void)
** elapsed for the iteration.
*/
static clock_t
-DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int num_arrays)
+DoNumSortIteration(long *array, unsigned int num_arrays)
{
clock_t start, stop;
unsigned long i;
@@ -108,12 +112,12 @@ DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int num_ar
/*
** Load up the array with random numbers
*/
- LoadNumArrayWithRand(arraybase, arraysize, num_arrays);
+ LoadNumArrayWithRand(array, num_arrays);
start = clock();
for (i = 0; i < num_arrays; i++) {
- NumHeapSort(&arraybase[i], 0L, arraysize - 1L);
+ NumHeapSort(&array[i], 0, ARRAY_SIZE - 1);
}
stop = clock();
@@ -127,7 +131,7 @@ DoNumSortIteration(long *arraybase, unsigned long arraysize, unsigned int num_ar
** Load up an array with random longs.
*/
static void
-LoadNumArrayWithRand(long *array, unsigned long arraysize, unsigned int num_arrays)
+LoadNumArrayWithRand(long *array, unsigned int num_arrays)
{
long i; /* Used for index */
long *darray; /* Destination array pointer */
@@ -136,7 +140,7 @@ LoadNumArrayWithRand(long *array, unsigned long arraysize, unsigned int num_arra
randnum(13);
/* Load up first array with randoms */
- for (i = 0; i < arraysize; i++) {
+ for (i = 0; i < ARRAY_SIZE; i++) {
array[i] = randnum(0);
}
@@ -146,8 +150,8 @@ LoadNumArrayWithRand(long *array, unsigned long arraysize, unsigned int num_arra
*/
darray = array;
while (--num_arrays) {
- darray += arraysize;
- memcpy(darray, array, arraysize * sizeof(long));
+ darray += ARRAY_SIZE;
+ memcpy(darray, array, ARRAY_SIZE * sizeof(long));
}
}