Files
hawk/t/t-008.c

88 lines
2.2 KiB
C
Raw Permalink Normal View History

2025-06-14 23:16:45 +09:00
#include <hawk-xma.h>
#include <hawk-std.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "tap.h"
2025-07-14 22:55:48 +09:00
#define NUM_ITERATIONS 10000
2025-06-14 23:27:55 +09:00
#define MIN_ALLOC_SIZE 16
#define MAX_ALLOC_SIZE 1024
2025-06-14 23:16:45 +09:00
#define OK_X(test) OK(test, #test)
2025-07-14 22:55:48 +09:00
static size_t random_size(hawk_oow_t max_alloc_size)
2025-06-14 23:16:45 +09:00
{
2025-07-14 22:55:48 +09:00
return MIN_ALLOC_SIZE + rand() % (max_alloc_size - MIN_ALLOC_SIZE + 1);
2025-06-14 23:16:45 +09:00
}
2025-07-14 22:55:48 +09:00
int main(int argc, char* argv[])
2025-06-14 23:16:45 +09:00
{
int test_bad = 0;
hawk_mmgr_t xma_mmgr;
2025-07-14 22:55:48 +09:00
hawk_oow_t num_iterations = NUM_ITERATIONS;
hawk_oow_t max_alloc_size = MAX_ALLOC_SIZE;
2025-06-14 23:16:45 +09:00
2025-06-14 23:27:55 +09:00
clock_t start_time, end_time;
double malloc_time = 0.0, free_time = 0.0;
void **ptr_array;
2025-07-15 23:48:07 +09:00
size_t i;
2025-06-14 23:27:55 +09:00
2025-07-14 22:55:48 +09:00
if (argc >= 3)
{
num_iterations = strtoul(argv[1], NULL, 10);
max_alloc_size = strtoul(argv[2], NULL, 10);
}
2025-06-14 23:16:45 +09:00
no_plan();
2025-07-14 22:55:48 +09:00
hawk_init_xma_mmgr(&xma_mmgr, num_iterations * max_alloc_size);
2025-06-14 23:16:45 +09:00
2025-06-14 23:27:55 +09:00
srand((unsigned int)time(NULL));
2025-06-14 23:16:45 +09:00
2025-07-14 22:55:48 +09:00
ptr_array = malloc(num_iterations * sizeof(void *));
2025-06-14 23:16:45 +09:00
OK_X (ptr_array != NULL);
if (!ptr_array) {
fprintf(stderr, "malloc failed for pointer array\n");
return -1;
}
start_time = clock();
2025-07-15 23:48:07 +09:00
for (i = 0; i < num_iterations; ++i) {
2025-07-14 22:55:48 +09:00
size_t size = random_size(max_alloc_size);
2025-06-14 23:16:45 +09:00
/*ptr_array[i] = malloc(size);*/
ptr_array[i] = HAWK_MMGR_ALLOC(&xma_mmgr, size);
if (!ptr_array[i]) {
fprintf(stderr, "malloc failed at iteration %zu\n", i);
test_bad = 1;
break;
}
}
OK_X (test_bad == 0);
if (test_bad) return -1;
end_time = clock();
malloc_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
start_time = clock();
2025-07-15 23:48:07 +09:00
for (i = 0; i < num_iterations; ++i) {
2025-06-14 23:16:45 +09:00
/*free(ptr_array[i]);*/
HAWK_MMGR_FREE(&xma_mmgr, ptr_array[i]);
}
end_time = clock();
free_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
2025-06-14 23:27:55 +09:00
free(ptr_array);
2025-06-14 23:16:45 +09:00
2025-07-14 22:55:48 +09:00
printf("Performed %lu allocations and frees - min alloc size %lu max_alloc_size %lu\n",
(unsigned long)num_iterations, (unsigned long)MIN_ALLOC_SIZE, (unsigned long)max_alloc_size);
2025-06-14 23:16:45 +09:00
printf("Total malloc time: %.6f seconds\n", malloc_time);
printf("Total free time : %.6f seconds\n", free_time);
2025-07-14 22:55:48 +09:00
printf("Average malloc time: %.9f seconds\n", malloc_time / num_iterations);
printf("Average free time : %.9f seconds\n", free_time / num_iterations);
2025-06-14 23:16:45 +09:00
2025-07-14 22:55:48 +09:00
hawk_fini_xma_mmgr(&xma_mmgr);
2025-06-14 23:16:45 +09:00
return exit_status();
}