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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
|
#ifdef DS_PERF
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NON_CONFORMING_SWPRINTFS
//#define STBDS_INTERNAL_SMALL_BUCKET // make 64-bit bucket fit both keys and hash bits
//#define STBDS_SIPHASH_2_4 // performance test 1_3 against 2_4
//#define STBDS_INTERNAL_BUCKET_START // don't bother offseting differently within bucket for different hash values
//#define STBDS_FLUSH_CACHE (1u<<20) // do this much memory traffic to flush the cache between some benchmarking measurements
#include <stdio.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define STB_DEFINE
#define STB_NO_REGISTRY
#include "../stb.h"
#endif
#ifdef DS_TEST
#define STBDS_UNIT_TESTS
#define STBDS_SMALL_BUCKET
#endif
#ifdef DS_STATS
#define STBDS_STATISTICS
#endif
#ifndef DS_PERF
#define STBDS_ASSERT assert
#include <assert.h>
#endif
#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)
{
char *temp=NULL;
stbds_unit_tests();
arrins(temp, 0, 'a');
arrins(temp, arrlen(temp), 'b');
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
//char *strdup(const char *foo) { return 0; }
//int stricmp(const char *a, const char *b) { return 0; }
//int strnicmp(const char *a, const char *b, size_t n) { return 0; }
unsigned __int64 t0, xsum, mn,mx,count;
void begin(void)
{
LARGE_INTEGER m;
QueryPerformanceCounter(&m);
t0 = m.QuadPart;
xsum = 0;
count = 0;
mx = 0;
mn = ~(unsigned __int64) 0;
}
void measure(void)
{
unsigned __int64 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;
xsum += t;
++count;
t0 = t1;
}
void dont_measure(void)
{
LARGE_INTEGER m;
QueryPerformanceCounter(&m);
t0 = m.QuadPart;
}
double timer;
double end(void)
{
LARGE_INTEGER m;
QueryPerformanceFrequency(&m);
if (count > 3) {
// discard the highest and lowest
xsum -= mn;
xsum -= mx;
count -= 2;
}
timer = (double) (xsum) / count / m.QuadPart * 1000;
return timer;
}
void build(int a, int b, int count, int step)
{
struct { int key,value; } *map=NULL;
int i,n;
for (i=0; i < a; ++i) {
n = i*step;
hmput(map,n,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);
int i;
for (i=0; i < a; ++i)
stb_idict_add(d, i*step, i+1);
measure();
churn_inserts = i;
stb_idict_destroy(d);
dont_measure();
}
void multibuild_stb(int a, int b, int count, int step, int tables)
{
stb_idict *d[50000];
int i,q;
for (q=0; q < tables; ++q)
d[q] = stb_idict_new_size(8);
dont_measure();
for (i=0; i < a; ++i)
for (q=0; q < tables; ++q)
stb_idict_add(d[q], i*step+q*771, i+1);
measure();
churn_inserts = i;
for (q=0; q < tables; ++q)
stb_idict_destroy(d[q]);
dont_measure();
}
int multisearch_stb(int a, int start, int end, int step, int tables)
{
stb_idict *d[50000];
int i,q,total=0,v;
for (q=0; q < tables; ++q)
d[q] = stb_idict_new_size(8);
for (q=0; q < tables; ++q)
for (i=0; i < a; ++i)
stb_idict_add(d[q], i*step+q*771, i+1);
dont_measure();
for (i=start; i < end; ++i)
for (q=0; q < tables; ++q)
if (stb_idict_get_flag(d[q], i*step+q*771, &v))
total += v;
measure();
churn_inserts = i;
for (q=0; q < tables; ++q)
stb_idict_destroy(d[q]);
dont_measure();
return total;
}
#endif
int multisearch(int a, int start, int end, int step, int tables)
{
struct { int key,value; } *hash[50000];
int i,q,total=0;
for (q=0; q < tables; ++q)
hash[q] = NULL;
for (q=0; q < tables; ++q)
for (i=0; i < a; ++i)
hmput(hash[q], i*step+q*771, i+1);
dont_measure();
for (i=start; i < end; ++i)
for (q=0; q < tables; ++q)
total += hmget(hash[q], i*step+q*771);
measure();
churn_inserts = i;
for (q=0; q < tables; ++q)
hmfree(hash[q]);
dont_measure();
return total;
}
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();
}
void multichurn4(int a, int b, int count, int include_startup, int tables)
{
struct { int key,value; } *map[50000];
int i,j,n,k,q;
for (q=0; q < tables; ++q)
map[q] = NULL;
dont_measure();
for (i=0; i < a; ++i)
for (q=0; q < tables; ++q)
hmput(map[q],i,i+1);
if (!include_startup)
dont_measure();
for (n=0; n < count; ++n) {
for (j=a; j < b; ++j,++i) {
for (q=0; q < tables; ++q)
hmput(map[q],i,i+1);
}
assert(hmlen(map[0]) == b);
for (j=a; j < b; ++j) {
k=i-j-1;
for (q=0; q < tables; ++q)
k = hmdel(map[q],k);
assert(k != 0);
}
assert(hmlen(map[0]) == a);
}
measure();
for (q=0; q < tables; ++q)
hmfree(map[q]);
churn_inserts = i * tables;
churn_deletes = (b-a) * n * tables;
dont_measure();
}
struct {
unsigned __int64 start;
unsigned __int64 end;
int table_size;
} mstats[32][4000];
const int first_step = 64;
const int last_step = 384-48; // 32M
void measure_build4(int step_log2)
{
double length;
int i,j,k=0;
int step = 1 << step_log2;
unsigned __int64 t0,t1;
struct { int key,value; } *map=NULL;
double rdtsc_scale;
begin();
t0 = __rdtsc();
mstats[0][0].start = __rdtsc();
for (i=0; i < 256; ++i) {
hmput(map,k,k+1);
k += step;
}
mstats[0][first_step-1].end = __rdtsc();
mstats[0][first_step-1].table_size = k >> step_log2;
for (j=first_step; j < last_step; ++j) {
for (i=0; i < (1<<(j>>4)); ++i) {
hmput(map, k,k+1);
k += step;
}
mstats[0][j].end = __rdtsc();
mstats[0][j].table_size = k >> step_log2;
}
t1 = __rdtsc();
measure();
hmfree(map);
length = end();
rdtsc_scale = length / (t1-t0) * 1000;
for (j=1; j < last_step; ++j)
mstats[0][j].start = mstats[0][0].start;
for (j=first_step-1; j < last_step; ++j) {
printf("%12.4f,%12d,%12d,0,0,0\n", (mstats[0][j].end - mstats[0][j].start) * rdtsc_scale, mstats[0][j].table_size, mstats[0][j].table_size);
}
}
#ifdef STBDS_FLUSH_CACHE
static int cache_index;
char dummy[8][STBDS_FLUSH_CACHE];
int flush_cache(void)
{
memmove(dummy[cache_index],dummy[cache_index]+1, sizeof(dummy[cache_index])-1);
cache_index = (cache_index+1)%8;
return dummy[cache_index][0];
}
#else
int flush_cache(void) { return 0; }
#endif
int measure_average_lookup4(int step_log2)
{
int total;
double length;
int i,j,k=0,q;
int step = 1 << step_log2;
unsigned __int64 t0,t1;
struct { int key,value; } *map=NULL;
double rdtsc_scale;
begin();
t0 = __rdtsc();
for (i=0; i < 128; ++i) {
hmput(map,k,k+1);
k += step;
}
for (j=first_step; j <= last_step; ++j) {
total += flush_cache();
mstats[0][j].start = __rdtsc();
for (q=i=0; i < 50000; ++i) {
total += hmget(map, q); // hit
if (++q == k) q = 0;
}
mstats[0][j].end = __rdtsc();
mstats[0][j].table_size = k;
total += flush_cache();
mstats[1][j].start = __rdtsc();
for (i=0; i < 50000; ++i) {
total += hmget(map, i+k); // miss
}
mstats[1][j].end = __rdtsc();
mstats[1][j].table_size = k;
// expand table
for (i=0; i < (1<<(j>>4)); ++i) {
hmput(map, k,k+1);
k += step;
}
}
t1 = __rdtsc();
measure();
hmfree(map);
length = end();
rdtsc_scale = length / (t1-t0) * 1000;
for (j=first_step; j <= last_step; ++j) {
// time,table_size,numins,numhit,nummiss,numperflush
printf("%12.4f,%12d,0,50000,0,0\n", (mstats[0][j].end - mstats[0][j].start) * rdtsc_scale, mstats[0][j].table_size);
}
for (j=first_step; j <= last_step; ++j) {
printf("%12.4f,%12d,0,0,50000,0\n", (mstats[1][j].end - mstats[1][j].start) * rdtsc_scale, mstats[1][j].table_size);
}
return total;
}
int measure_worst_lookup4_a(int step_log2)
{
int total;
double length;
int i,j,k=0,q,worst_q,n,z,attempts;
int step = 1 << step_log2;
unsigned __int64 t0,t1;
unsigned __int64 m0,m1,worst;
struct { int key,value; } *map=NULL;
double rdtsc_scale;
begin();
t0 = __rdtsc();
memset(mstats, 0, sizeof(mstats));
for (j=first_step; j <= last_step; ++j)
mstats[0][j].end = mstats[1][j].end = ~(unsigned __int64) 0;
for(attempts=0; attempts < 2; ++attempts) {
k = 0;
stbds_rand_seed(0); // force us to get the same table every time
for (i=0; i < 128; ++i) {
hmput(map,k,k+1);
k += step;
}
for (j=first_step; j <= last_step; ++j) {
unsigned __int64 times[32];
// find the worst hit time
for (z=0; z < 2; ++z) { // try the bisectioning measurement 4 times
worst = 0;
for (n=0; n < 10; ++n) { // test 400 keys total
// find the worst time to hit 20 keys
q=0;
worst_q = 0;
total += flush_cache();
m0 = __rdtsc();
for (i=0; i < 20; ++i) {
total += hmget(map, q); // hit
if (++q == k) q = 0;
}
m1 = __rdtsc();
// for each n, check if this is the worst lookup we've seen
if (m1 - m0 > worst) {
worst = m1-m0;
worst_q = q - i;
if (worst_q < 0) q += k;
}
}
// after 400 keys, take the worst 20 keys, and try each one
worst = 0;
q = worst_q;
for (i=0; i < 20; ++i) {
total += flush_cache();
m0 = __rdtsc();
total += hmget(map, q); // hit
m1 = __rdtsc();
if (m1 - m0 > worst)
worst = m1-m0;
if (++q == k) q = 0;
}
times[z] = worst;
}
// find the worst time in the bunch
worst = 0;
for (i=0; i < z; ++i)
if (times[i] > worst)
worst = times[i];
// take the best of 'attempts', to discard outliers
if (worst < mstats[0][j].end)
mstats[0][j].end = worst;
mstats[0][j].start = 0;
mstats[0][j].table_size = k >> step_log2;
// find the worst miss time
for (z=0; z < 8; ++z) { // try the bisectioning measurement 8 times
worst = 0;
for (n=0; n < 20; ++n) { // test 400 keys total
// find the worst time to hit 20 keys
q=k;
worst_q = 0;
total += flush_cache();
m0 = __rdtsc();
for (i=0; i < 20; ++i) {
total += hmget(map, q); // hit
}
m1 = __rdtsc();
// for each n, check if this is the worst lookup we've seen
if (m1 - m0 > worst) {
worst = m1-m0;
worst_q = q - i;
}
}
// after 400 keys, take the worst 20 keys, and try each one
worst = 0;
q = worst_q;
for (i=0; i < 20; ++i) {
total += flush_cache();
m0 = __rdtsc();
total += hmget(map, q); // hit
m1 = __rdtsc();
if (m1 - m0 > worst)
worst = m1-m0;
}
times[z] = worst;
}
// find the worst time in the bunch
worst = 0;
for (i=0; i < z; ++i)
if (times[i] > worst)
worst = times[i];
if (worst < mstats[1][j].end)
mstats[1][j].end = worst;
mstats[1][j].start = 0;
mstats[1][j].table_size = k >> step_log2;
// expand table
for (i=0; i < (1<<(j>>4)); ++i) {
hmput(map, k,k+1);
k += step;
}
}
hmfree(map);
}
t1 = __rdtsc();
measure();
length = end();
rdtsc_scale = length / (t1-t0) * 1000;
for (j=first_step; j <= last_step; ++j) {
printf("%12.4f,%12d,0,1,0,1\n", (mstats[0][j].end - mstats[0][j].start) * rdtsc_scale, mstats[0][j].table_size);
}
for (j=first_step; j <= last_step; ++j) {
printf("%12.4f,%12d,0,0,1,1\n", (mstats[1][j].end - mstats[1][j].start) * rdtsc_scale, mstats[1][j].table_size);
}
return total;
}
int measure_worst_lookup4_b(int step_log2)
{
int total;
double length;
int i,j,k=0,q,worst_q,n,z,attempts;
int step = 1 << step_log2;
unsigned __int64 t0,t1;
unsigned __int64 m0,m1,worst;
struct { int key,value; } *map=NULL;
double rdtsc_scale;
begin();
t0 = __rdtsc();
memset(mstats, 0, sizeof(mstats));
for (j=first_step; j <= last_step; ++j)
mstats[0][j].end = mstats[1][j].end = ~(unsigned __int64) 0;
k = 0;
stbds_rand_seed(0); // force us to get the same table every time
for (i=0; i < 128; ++i) {
hmput(map,k,k+1);
k += step;
}
for (j=first_step; j <= last_step; ++j) {
unsigned __int64 times[32];
// find the worst hit time
for (z=0; z < 8; ++z) { // try this 8 times
worst = 0;
q=0;
for (i=0; i < 5000; ++i) {
total += hmget(map, q);
m0 = __rdtsc();
total += hmget(map, q);
m1 = __rdtsc();
if (m1 - m0 > worst) {
worst = m1-m0;
worst_q = q - i;
}
if (++q == k) q = 0;
}
// now retry with the worst one, but find the shortest time for it
worst = ~(unsigned __int64) 0;
for (i=0; i < 4; ++i) {
total += flush_cache();
m0 = __rdtsc();
total += hmget(map,worst_q);
m1 = __rdtsc();
if (m1-m0 < worst)
worst = m1-m0;
}
times[z] = worst;
}
// find the worst of those
worst = 0;
for (i=0; i < z; ++i)
if (times[i] > worst)
worst = times[i];
mstats[0][j].start = 0;
mstats[0][j].end = worst;
mstats[0][j].table_size = k;
// find the worst miss time
for (z=0; z < 8; ++z) { // try this 8 times
worst = 0;
q=k;
for (i=0; i < 5000; ++i) {
total += hmget(map, q);
m0 = __rdtsc();
total += hmget(map, q);
m1 = __rdtsc();
if (m1 - m0 > worst) {
worst = m1-m0;
worst_q = q - i;
}
//printf("%6llu ", m1-m0);
}
// now retry with the worst one, but find the shortest time for it
worst = ~(unsigned __int64) 0;
for (i=0; i < 4; ++i) {
total += flush_cache();
m0 = __rdtsc();
total += hmget(map,worst_q);
m1 = __rdtsc();
if (m1-m0 < worst)
worst = m1-m0;
}
times[z] = worst;
}
// find the worst of those
worst = 0;
for (i=0; i < z; ++i)
if (times[i] > worst)
worst = times[i];
mstats[1][j].start = 0;
mstats[1][j].end = worst;
mstats[1][j].table_size = k;
// expand table
for (i=0; i < (1<<(j>>4)); ++i) {
hmput(map, k,k+1);
k += step;
}
}
hmfree(map);
t1 = __rdtsc();
measure();
length = end();
rdtsc_scale = length / (t1-t0) * 1000;
for (j=first_step+1; j <= last_step; ++j) {
printf("%12.4f,%12d,0,1,0,1\n", (mstats[0][j].end - mstats[0][j].start) * rdtsc_scale, mstats[0][j].table_size);
}
for (j=first_step+1; j <= last_step; ++j) {
printf("%12.4f,%12d,0,0,1,1\n", (mstats[1][j].end - mstats[1][j].start) * rdtsc_scale, mstats[1][j].table_size);
}
return total;
}
int measure_uncached_lookup4(int step_log2)
{
int total;
double length;
int i,j,k=0,q;
int step = 1 << step_log2;
unsigned __int64 t0,t1;
struct { int key,value; } *map=NULL;
double rdtsc_scale;
begin();
t0 = __rdtsc();
map = NULL;
for (i=0; i < 128; ++i) {
hmput(map,k,k+1);
k += step;
}
for (j=first_step; j <= last_step; ++j) {
mstats[0][j].start = __rdtsc();
mstats[0][j].end = 0;
for (q=i=0; i < 512; ++i) {
if ((i & 3) == 0) {
mstats[0][j].end += __rdtsc();
total += flush_cache();
mstats[0][j].start += __rdtsc();
}
total += hmget(map, q); // hit
if (++q == k) q = 0;
}
mstats[0][j].end += __rdtsc();
mstats[0][j].table_size = k;
total += flush_cache();
mstats[1][j].end = 0;
mstats[1][j].start = __rdtsc();
for (i=0; i < 512; ++i) {
if ((i & 3) == 0) {
mstats[1][j].end += __rdtsc();
total += flush_cache();
mstats[1][j].start += __rdtsc();
}
total += hmget(map, i+k); // miss
}
mstats[1][j].end += __rdtsc();
mstats[1][j].table_size = k;
// expand table
for (i=0; i < (1<<(j>>4)); ++i) {
hmput(map, k,k+1);
k += step;
}
}
hmfree(map);
t1 = __rdtsc();
measure();
length = end();
rdtsc_scale = length / (t1-t0) * 1000;
for (j=first_step; j <= last_step; ++j) {
printf("%12.4f,%12d,0,512,0,4\n", (mstats[0][j].end - mstats[0][j].start) * rdtsc_scale, mstats[0][j].table_size);
}
for (j=first_step; j <= last_step; ++j) {
printf("%12.4f,%12d,0,0,512,4\n", (mstats[1][j].end - mstats[1][j].start) * rdtsc_scale, mstats[1][j].table_size);
}
return total;
}
int main(int arg, char **argv)
{
int n,s,w;
double worst = 0;
printf("# size_t=%d,", (int) sizeof(size_t));
// number of cache-lines
#ifdef STBDS_SMALL_BUCKET
printf("cacheline=%d,", 1);
#else
printf("cacheline=%d,", sizeof(size_t)==8 ? 2 : 1);
#endif
#ifdef STBDS_FLUSH_CACHE
printf("%d,", (int) stbds_log2(STBDS_FLUSH_CACHE));
#else
printf("0,");
#endif
#ifdef STBDS_BUCKET_START // don't bother offseting differently within bucket for different hash values
printf("STBDS_BUCKET_START,");
#else
printf(",");
#endif
#ifdef STBDS_SIPHASH_2_4
printf("STBDS_SIPHASH_2_4,");
#else
printf(",");
#endif
printf("\n");
measure_worst_lookup4_b(0);
//measure_worst_lookup4_a(0);
measure_average_lookup4(0);
measure_uncached_lookup4(0);
measure_build4(0);
return 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
#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 < 20; ++n) { multisearch_stb(2000,0,2000,1,1000); } end(); printf(" // %7.2fms : 2,000,000 hits on 1,000 2K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 10; ++n) { multisearch_stb(20000,0,2000,1,1000); } end(); printf(" // %7.2fms : 2,000,000 hits on 1,000 20K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 6; ++n) { multisearch_stb(200000,0,2000,1,1000); } end(); printf(" // %7.2fms : 2,000,000 hits on 1,000 200K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 2; ++n) { multisearch_stb(2000000,0,20000,1,100); } end(); printf(" // %7.2fms : 2,000,000 hits on 100 2M table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 20; ++n) { multisearch (2000,0,2000,1,1000); } end(); printf(" // %7.2fms : 2,000,000 hits on 1,000 2K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 10; ++n) { multisearch (20000,0,2000,1,1000); } end(); printf(" // %7.2fms : 2,000,000 hits on 1,000 20K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 6; ++n) { multisearch (200000,0,2000,1,1000); } end(); printf(" // %7.2fms : 2,000,000 hits on 1,000 200K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 2; ++n) { multisearch (2000000,0,20000,1,100); } end(); printf(" // %7.2fms : 2,000,000 hits on 100 2M table w/ 4-byte key\n", timer);
#if 1
begin(); for (n=0; n < 2; ++n) { multibuild_stb(2000,0,0,1,10000); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 10,000 2K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 2; ++n) { multibuild_stb(20000,0,0,1,1000); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 1,000 20K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 2; ++n) { multibuild_stb(200000,0,0,1,100); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 100 200K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 2; ++n) { multibuild_stb(2000000,0,0,1,10); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 10 2M table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 2; ++n) { multichurn4(2000,0,0,1,10000); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 10,000 2K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 2; ++n) { multichurn4(20000,0,0,1,1000); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 1,000 20K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 2; ++n) { multichurn4(200000,0,0,1,100); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 100 200K table w/ 4-byte key\n", timer);
begin(); for (n=0; n < 2; ++n) { multichurn4(2000000,0,0,1,10); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 10 2M table w/ 4-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 < 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);
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
|