67 lines
1.5 KiB
C
67 lines
1.5 KiB
C
|
/* 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[])
|
|||
|
{
|
|||
|
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;
|
|||
|
}
|