dev/c/realloc_speed_test/realloc_speed_test_cpp.cpp

38 lines
803 B
C++

// time ./realloc_speed_test_cpp 50000 10000 100000
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <string>
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]);
std::vector<std::string> v(nobj);
size_t i, idx, sz;
for (i = 0; i < iterations; i++) {
idx = rand() % nobj;
sz = i;//(size_t)(rand() % (int)maxsz);
try {
v[idx].resize(sz);
} catch(...) {
fprintf(stderr, "Alloc error at i = %lu\n", i);
exit(-1);
}
}
printf("Success, iterations = %lu, nobj = %lu, maxsz = %lu\n", iterations, nobj, maxsz);
return EXIT_SUCCESS;
}