dev/c/zmalloc/zmalloc_test.c

75 lines
1.7 KiB
C
Raw Normal View History

/* time ./zmalloc_test 60000 100000 1
* примерно в 2 раза бьёт realloc
* и в 5-6 раз бьёт std::string
* Не оптимизировал пока что, потом будет ещё круче =)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zmalloc.h"
int main(int argc, char *argv[])
{
2011-06-10 11:59:15 +04:00
// тест zmalloc_getinfo()
//~ void *p1 = zmalloc(48);
//~ struct zmalloc_s zms;
//~ zmalloc_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;
}