dev/c/realloc_speed_test/realloc_speed_test_glib.c

45 lines
893 B
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "xalloc.h"
int main(int argc, char *argv[])
{
if (argc != 3) {
fputs("Usage: realloc_speed_test_glib iterations nobj\n", stderr);
exit(-1);
}
size_t iterations = (size_t)atoi(argv[1]);
size_t nobj = (size_t)atoi(argv[2]);
GString **p = malloc(sizeof(GString *) * nobj);
memset(p, 0, sizeof(void *) * nobj);
size_t i, idx, sz;
for (i = 0; i < nobj; i++)
p[i] = g_string_new(0);
for (i = 0; i < iterations; i++) {
idx = (size_t)(rand() % (int)nobj);
sz = i;
p[idx] = g_string_append_c(p[idx], 'c');//realloc(p[idx], sz);
if (sz && !p[idx]) {
fprintf(stderr, "Alloc error at i = %lu\n", i);
exit(-1);
}
}
for (i = 0; i < nobj; i++)
g_string_free(p[i], FALSE);
free(p);
printf("Success, iterations = %lu, nobj = %lu\n", iterations, nobj);
return EXIT_SUCCESS;
}