dev/c/zalloc/zalloc_test.c

75 lines
1.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* time ./zalloc_test 60000 100000 1
* примерно в 2 раза бьёт realloc
* и в 5-6 раз бьёт std::string
* Не оптимизировал пока что, потом будет ещё круче =)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zalloc.h"
int main(int argc, char *argv[])
{
// тест zalloc_getinfo()
//~ void *p1 = zmalloc(48);
//~ struct zalloc_s zms;
//~ zalloc_getinfo(p1, &zms);
//~ printf("all=%lu, len=%lu\n", zms.all, zms.len);
//~ zfree(&p1);
if (argc != 4) {
fputs("Usage: realloc_speed_test_cpp iterations nobj maxsz\n", stderr);
exit(-1);
}
size_t iterations = (size_t)atoi(argv[1]);
size_t nobj = (size_t)atoi(argv[2]);
size_t maxsz = (size_t)atoi(argv[3]);
void **p = malloc(sizeof(void *) * nobj);
memset(p, 0, sizeof(void *) * nobj);
size_t i, idx, sz;
for (i = 0; i < iterations; i++) {
idx = (size_t)(rand() % (int)nobj);
sz = i;//(size_t)(rand() % (int)maxsz);
zrealloc(&p[idx], sz);
if (sz && !p[idx]) {
fprintf(stderr, "Alloc error at i = %lu\n", i);
exit(-1);
}
}
for (i = 0; i < nobj; i++)
zfree(&p[i]);
printf("Success, iterations = %lu, nobj = %lu, maxsz = %lu\n", iterations, nobj, maxsz);
return EXIT_SUCCESS;
//~
//~ if (argc != 2) {
//~ fputs("Usage: realloc_speed_test iterations\n", stderr);
//~ exit(-1);
//~ }
//~
//~ size_t n = (size_t)atoi(argv[1]);
//~
//~ size_t i;
//~ void *m = NULL;
//~ for (i = 1; i < n; i++) {
//~ zrealloc(&m, i);
//~ if (!m) {
//~ fprintf(stderr, "Alloc error at i = %lu\n", i);
//~ exit(-1);
//~ }
//~ }
//~ zfree(m);
//~
//~ printf("Success, n = %lu\n", n);
//~
//~ return EXIT_SUCCESS;
}