1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
#include <stdio.h>
#ifdef DS_TEST
#define STBDS_UNIT_TESTS
#endif
#ifdef DS_STATS
#define STBDS_STATISTICS
#endif
#ifndef DS_PERF
#define STBDS_ASSERT assert
#include <assert.h>
#endif
//#define STBDS_SIPHASH_2_4
#define STB_DS_IMPLEMENTATION
#include "../stb_ds.h"
size_t churn_inserts, churn_deletes;
void churn(int a, int b, int count)
{
struct { int key,value; } *map=NULL;
int i,j,n,k;
for (i=0; i < a; ++i)
hmput(map,i,i+1);
for (n=0; n < count; ++n) {
for (j=a; j < b; ++j,++i) {
hmput(map,i,i+1);
}
assert(hmlen(map) == b);
for (j=a; j < b; ++j) {
k=i-j-1;
k = hmdel(map,k);
assert(k != 0);
}
assert(hmlen(map) == a);
}
hmfree(map);
churn_inserts = i;
churn_deletes = (b-a) * n;
}
#ifdef DS_TEST
#include <stdio.h>
int main(int argc, char **argv)
{
stbds_unit_tests();
churn(0,100,1);
churn(3,7,50000);
churn(3,15,50000);
churn(16, 48, 25000);
churn(10, 15, 25000);
churn(200,500, 5000);
churn(2000,5000, 500);
churn(20000,50000, 50);
printf("Ok!");
return 0;
}
#endif
#ifdef DS_STATS
#define MAX(a,b) ((a) > (b) ? (a) : (b))
size_t max_hit_probes, max_miss_probes, total_put_probes, total_miss_probes, churn_misses;
void churn_stats(int a, int b, int count)
{
struct { int key,value; } *map=NULL;
int i,j,n,k;
churn_misses = 0;
for (i=0; i < a; ++i) {
hmput(map,i,i+1);
max_hit_probes = MAX(max_hit_probes, stbds_hash_probes);
total_put_probes += stbds_hash_probes;
stbds_hash_probes = 0;
}
for (n=0; n < count; ++n) {
for (j=a; j < b; ++j,++i) {
hmput(map,i,i+1);
max_hit_probes = MAX(max_hit_probes, stbds_hash_probes);
total_put_probes += stbds_hash_probes;
stbds_hash_probes = 0;
}
for (j=0; j < (b-a)*10; ++j) {
k=i+j;
(void) hmgeti(map,k); // miss
max_miss_probes = MAX(max_miss_probes, stbds_hash_probes);
total_miss_probes += stbds_hash_probes;
stbds_hash_probes = 0;
++churn_misses;
}
assert(hmlen(map) == b);
for (j=a; j < b; ++j) {
k=i-j-1;
k = hmdel(map,k);
stbds_hash_probes = 0;
assert(k);
}
assert(hmlen(map) == a);
}
hmfree(map);
churn_inserts = i;
churn_deletes = (b-a) * n;
}
void reset_stats(void)
{
stbds_array_grow=0,
stbds_hash_grow=0;
stbds_hash_shrink=0;
stbds_hash_rebuild=0;
stbds_hash_probes=0;
stbds_hash_alloc=0;
stbds_rehash_probes=0;
stbds_rehash_items=0;
max_hit_probes = 0;
max_miss_probes = 0;
total_put_probes = 0;
total_miss_probes = 0;
}
void print_churn_probe_stats(char *str)
{
printf("Probes: %3d max hit, %3d max miss, %4.2f avg hit, %4.2f avg miss: %s\n",
(int) max_hit_probes, (int) max_miss_probes, (float) total_put_probes / churn_inserts, (float) total_miss_probes / churn_misses, str);
reset_stats();
}
int main(int arg, char **argv)
{
churn_stats(0,500000,1); print_churn_probe_stats("Inserting 500000 items");
churn_stats(0,500000,1); print_churn_probe_stats("Inserting 500000 items");
churn_stats(0,500000,1); print_churn_probe_stats("Inserting 500000 items");
churn_stats(0,500000,1); print_churn_probe_stats("Inserting 500000 items");
churn_stats(49000,50000,500); print_churn_probe_stats("Deleting/Inserting 500000 items");
churn_stats(49000,50000,500); print_churn_probe_stats("Deleting/Inserting 500000 items");
churn_stats(49000,50000,500); print_churn_probe_stats("Deleting/Inserting 500000 items");
churn_stats(49000,50000,500); print_churn_probe_stats("Deleting/Inserting 500000 items");
return 0;
}
#endif
#ifdef DS_PERF
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define STB_DEFINE
#define STB_NO_REGISTRY
//#include "../stb.h"
size_t t0, sum, mn,mx,count;
void begin(void)
{
size_t t0;
LARGE_INTEGER m;
QueryPerformanceCounter(&m);
t0 = m.QuadPart;
sum = 0;
count = 0;
mx = 0;
mn = ~(size_t) 0;
}
void measure(void)
{
size_t t1, t;
LARGE_INTEGER m;
QueryPerformanceCounter(&m);
t1 = m.QuadPart;
t = t1-t0;
if (t1 < t0)
printf("ALERT: QueryPerformanceCounter was unordered!\n");
if (t < mn) mn = t;
if (t > mx) mx = t;
sum += t;
++count;
t0 = t1;
}
void dont_measure(void)
{
size_t t1, t;
LARGE_INTEGER m;
QueryPerformanceCounter(&m);
t0 = m.QuadPart;
}
double timer;
void end(void)
{
LARGE_INTEGER m;
QueryPerformanceFrequency(&m);
if (count > 3) {
// discard the highest and lowest
sum -= mn;
sum -= mx;
count -= 2;
}
timer = (double) (sum) / count / m.QuadPart * 1000;
}
void build(int a, int b, int count, int step)
{
struct { int key,value; } *map=NULL;
int i,j,n,k;
for (i=0; i < a; ++i)
hmput(map,i*step,i+1);
measure();
churn_inserts = i;
hmfree(map);
dont_measure();
}
#ifdef STB__INCLUDE_STB_H
void build_stb(int a, int b, int count, int step)
{
stb_idict *d = stb_idict_new_size(8);
struct { int key,value; } *map=NULL;
int i,j,n,k;
for (i=0; i < a; ++i)
stb_idict_add(d, i*step, i+1);
measure();
churn_inserts = i;
stb_idict_destroy(d);
dont_measure();
}
#endif
void churn_skip(unsigned int a, unsigned int b, int count)
{
struct { unsigned int key,value; } *map=NULL;
unsigned int i,j,n,k;
for (i=0; i < a; ++i)
hmput(map,i,i+1);
dont_measure();
for (n=0; n < count; ++n) {
for (j=a; j < b; ++j,++i) {
hmput(map,i,i+1);
}
assert(hmlen(map) == b);
for (j=a; j < b; ++j) {
k=i-j-1;
k = hmdel(map,k);
assert(k != 0);
}
assert(hmlen(map) == a);
}
measure();
churn_inserts = i;
churn_deletes = (b-a) * n;
hmfree(map);
dont_measure();
}
typedef struct { int n[8]; } str32;
void churn32(int a, int b, int count, int include_startup)
{
struct { str32 key; int value; } *map=NULL;
int i,j,n;
str32 key = { 0 };
for (i=0; i < a; ++i) {
key.n[0] = i;
hmput(map,key,i+1);
}
if (!include_startup)
dont_measure();
for (n=0; n < count; ++n) {
for (j=a; j < b; ++j,++i) {
key.n[0] = i;
hmput(map,key,i+1);
}
assert(hmlen(map) == b);
for (j=a; j < b; ++j) {
key.n[0] = i-j-1;
hmdel(map,key);
}
assert(hmlen(map) == a);
}
measure();
hmfree(map);
churn_inserts = i;
churn_deletes = (b-a) * n;
dont_measure();
}
typedef struct { int n[32]; } str256;
void churn256(int a, int b, int count, int include_startup)
{
struct { str256 key; int value; } *map=NULL;
int i,j,n;
str256 key = { 0 };
for (i=0; i < a; ++i) {
key.n[0] = i;
hmput(map,key,i+1);
}
if (!include_startup)
dont_measure();
for (n=0; n < count; ++n) {
for (j=a; j < b; ++j,++i) {
key.n[0] = i;
hmput(map,key,i+1);
}
assert(hmlen(map) == b);
for (j=a; j < b; ++j) {
key.n[0] = i-j-1;
hmdel(map,key);
}
assert(hmlen(map) == a);
}
measure();
hmfree(map);
churn_inserts = i;
churn_deletes = (b-a) * n;
dont_measure();
}
void churn8(int a, int b, int count, int include_startup)
{
struct { size_t key,value; } *map=NULL;
int i,j,n,k;
for (i=0; i < a; ++i)
hmput(map,i,i+1);
if (!include_startup)
dont_measure();
for (n=0; n < count; ++n) {
for (j=a; j < b; ++j,++i) {
hmput(map,i,i+1);
}
assert(hmlen(map) == b);
for (j=a; j < b; ++j) {
k=i-j-1;
k = hmdel(map,k);
assert(k != 0);
}
assert(hmlen(map) == a);
}
measure();
hmfree(map);
churn_inserts = i;
churn_deletes = (b-a) * n;
dont_measure();
}
int main(int arg, char **argv)
{
int n,s,w;
double worst = 0;
#if 0
begin(); for (n=0; n < 2000; ++n) { build_stb(2000,0,0,1); } end(); printf(" // %7.2fms : 2,000 inserts creating 2K table\n", timer);
begin(); for (n=0; n < 500; ++n) { build_stb(20000,0,0,1); } end(); printf(" // %7.2fms : 20,000 inserts creating 20K table\n", timer);
begin(); for (n=0; n < 100; ++n) { build_stb(200000,0,0,1); } end(); printf(" // %7.2fms : 200,000 inserts creating 200K table\n", timer);
begin(); for (n=0; n < 10; ++n) { build_stb(2000000,0,0,1); } end(); printf(" // %7.2fms : 2,000,000 inserts creating 2M table\n", timer);
begin(); for (n=0; n < 5; ++n) { build_stb(20000000,0,0,1); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 20M table\n", timer);
#endif
begin(); for (n=0; n < 2000; ++n) { churn8(2000,0,0,1); } end(); printf(" // %7.2fms : 2,000 inserts creating 2K table w/ 8-byte key\n", timer);
begin(); for (n=0; n < 500; ++n) { churn8(20000,0,0,1); } end(); printf(" // %7.2fms : 20,000 inserts creating 20K table w/ 8-byte key\n", timer);
begin(); for (n=0; n < 100; ++n) { churn8(200000,0,0,1); } end(); printf(" // %7.2fms : 200,000 inserts creating 200K table w/ 8-byte key\n", timer);
begin(); for (n=0; n < 10; ++n) { churn8(2000000,0,0,1); } end(); printf(" // %7.2fms : 2,000,000 inserts creating 2M table w/ 8-byte key\n", timer);
begin(); for (n=0; n < 5; ++n) { churn8(20000000,0,0,1); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 20M table w/ 8-byte key\n", timer);
#if 0
begin(); for (n=0; n < 2000; ++n) { churn32(2000,0,0,1); } end(); printf(" // %7.2fms : 2,000 inserts creating 2K table w/ 32-byte key\n", timer);
begin(); for (n=0; n < 500; ++n) { churn32(20000,0,0,1); } end(); printf(" // %7.2fms : 20,000 inserts creating 20K table w/ 32-byte key\n", timer);
begin(); for (n=0; n < 100; ++n) { churn32(200000,0,0,1); } end(); printf(" // %7.2fms : 200,000 inserts creating 200K table w/ 32-byte key\n", timer);
begin(); for (n=0; n < 10; ++n) { churn32(2000000,0,0,1); } end(); printf(" // %7.2fms : 2,000,000 inserts creating 2M table w/ 32-byte key\n", timer);
begin(); for (n=0; n < 5; ++n) { churn32(20000000,0,0,1); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 20M table w/ 32-byte key\n", timer);
begin(); for (n=0; n < 2000; ++n) { churn256(2000,0,0,1); } end(); printf(" // %7.2fms : 2,000 inserts creating 2K table w/ 256-byte key\n", timer);
begin(); for (n=0; n < 500; ++n) { churn256(20000,0,0,1); } end(); printf(" // %7.2fms : 20,000 inserts creating 20K table w/ 256-byte key\n", timer);
begin(); for (n=0; n < 100; ++n) { churn256(200000,0,0,1); } end(); printf(" // %7.2fms : 200,000 inserts creating 200K table w/ 256-byte key\n", timer);
begin(); for (n=0; n < 10; ++n) { churn256(2000000,0,0,1); } end(); printf(" // %7.2fms : 2,000,000 inserts creating 2M table w/ 256-byte key\n", timer);
begin(); for (n=0; n < 5; ++n) { churn256(20000000,0,0,1); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 20M table w/ 256-byte key\n", timer);
#endif
begin(); for (n=0; n < 2000; ++n) { build(2000,0,0,1); } end(); printf(" // %7.2fms : 2,000 inserts creating 2K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 500; ++n) { build(20000,0,0,1); } end(); printf(" // %7.2fms : 20,000 inserts creating 20K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 100; ++n) { build(200000,0,0,1); } end(); printf(" // %7.2fms : 200,000 inserts creating 200K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 10; ++n) { build(2000000,0,0,1); } end(); printf(" // %7.2fms : 2,000,000 inserts creating 2M table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 5; ++n) { build(20000000,0,0,1); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 20M table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 60; ++n) { churn_skip(2000,2100,5000); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 2K table\n", timer);
begin(); for (n=0; n < 30; ++n) { churn_skip(20000,21000,500); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 20K table\n", timer);
begin(); for (n=0; n < 15; ++n) { churn_skip(200000,201000,500); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 200K table\n", timer);
begin(); for (n=0; n < 8; ++n) { churn_skip(2000000,2001000,500); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 2M table\n", timer);
begin(); for (n=0; n < 5; ++n) { churn_skip(20000000,20001000,500); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 20M table\n", timer);
begin(); for (n=0; n < 1; ++n) { churn_skip(200000000u,200001000u,500); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 200M table\n", timer);
// even though the above measures a roughly fixed amount of work, we still have to build the table n times, hence the fewer measurements each time
begin(); for (n=0; n < 60; ++n) { churn_skip(1000,3000,250); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 2K table\n", timer);
begin(); for (n=0; n < 15; ++n) { churn_skip(10000,30000,25); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 20K table\n", timer);
begin(); for (n=0; n < 7; ++n) { churn_skip(100000,300000,10); } end(); printf(" // %7.2fms : 2,000,000 inserts & deletes in 200K table\n", timer);
begin(); for (n=0; n < 2; ++n) { churn_skip(1000000,3000000,10); } end(); printf(" // %7.2fms : 20,000,000 inserts & deletes in 2M table\n", timer);
// search for bad intervals.. in practice this just seems to measure execution variance
for (s = 2; s < 64; ++s) {
begin(); for (n=0; n < 50; ++n) { build(200000,0,0,s); } end();
if (timer > worst) {
worst = timer;
w = s;
}
}
for (; s <= 1024; s *= 2) {
begin(); for (n=0; n < 50; ++n) { build(200000,0,0,s); } end();
if (timer > worst) {
worst = timer;
w = s;
}
}
printf(" // %7.2fms(%d) : Worst time from inserting 200,000 items with spacing %d.\n", worst, w, w);
return 0;
}
#endif
|