#define IMPORT_CORE #include #include "core/HashMap.h" #include "core/Random.h" #include "core/Utility.h" static i64 testSearch(const HashMap* m) { i64 nanos = coreGetNanos(); volatile int sum = 0; for(int i = 0; i < 10000; i++) { for(int k = -5000; k < 5000; k++) { const int* s = coreSearchTypedHashMapKey(m, int, i + k, int); if(s != nullptr) { sum = sum + *s; } } } return coreGetNanos() - nanos; } static i64 testEmptySearch(const HashMap* m) { i64 nanos = coreGetNanos(); volatile int sum = 0; for(int i = 0; i < 100'000'000; i++) { const int* s = coreSearchTypedHashMapKey(m, int, -i, int); if(s != nullptr) { sum = sum + *s; } } return coreGetNanos() - nanos; } static void fillOrder(HashMap* m) { i64 nanos = coreGetNanos(); for(int i = 0; i < 10000; i++) { corePutTypedHashMapPair(m, int, i, int, i* i); } printf("Fill Order: %ldns\n", coreGetNanos() - nanos); } static void fillChaos(HashMap* m) { i64 nanos = coreGetNanos(); Random random; coreInitRandom(&random, 0); for(int i = 0; i < 10000; i++) { int r = coreRandomI32(&random, 0, 10000); corePutTypedHashMapPair(m, int, r, int, r* r); } printf("Fill Chaos: %ldns\n", coreGetNanos() - nanos); } static i64 average(HashMap* m, i64 (*f)(const HashMap* m), int n) { i64 sum = 0; for(int i = 0; i < n; i++) { sum += f(m); } return (i64)(sum / (n * 1'000'000)); } static void order(int n) { HashMap m; coreInitHashMap(&m, sizeof(int), sizeof(int)); fillOrder(&m); puts("Order Probing"); printf("Search | %ld ms\n", average(&m, testSearch, n)); printf("EmptySearch | %ld ms\n", average(&m, testEmptySearch, n)); coreDestroyHashMap(&m); } static void chaos(int n) { HashMap m; coreInitHashMap(&m, sizeof(int), sizeof(int)); fillChaos(&m); puts("Chaos Probing"); printf("Search | %ld ms\n", average(&m, testSearch, n)); printf("EmptySearch | %ld ms\n", average(&m, testEmptySearch, n)); coreDestroyHashMap(&m); } int main() { order(3); chaos(3); return 0; }