#include #include "core/HashMap.h" #include "core/Random.h" #include "core/Utility.h" static i64 testSearch(const CoreHashMap* 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 = coreHashMapSearchC(m, int, i + k, int); if(s != nullptr) { sum = sum + *s; } } } return coreGetNanos() - nanos; } static i64 testEmptySearch(const CoreHashMap* m) { i64 nanos = coreGetNanos(); volatile int sum = 0; for(int i = 0; i < 100'000'000; i++) { const int* s = coreHashMapSearchC(m, int, -i, int); if(s != nullptr) { sum = sum + *s; } } return coreGetNanos() - nanos; } static void fillOrder(CoreHashMap* m) { i64 nanos = coreGetNanos(); for(int i = 0; i < 10000; i++) { coreHashMapPut(m, int, i, int, i* i); } printf("Fill Order: %ldns\n", coreGetNanos() - nanos); } static void fillChaos(CoreHashMap* m) { i64 nanos = coreGetNanos(); CoreRandom random; coreInitRandom(&random, 0); for(int i = 0; i < 10000; i++) { int r = coreRandomI32(&random, 0, 10000); coreHashMapPut(m, int, r, int, r* r); } printf("Fill Chaos: %ldns\n", coreGetNanos() - nanos); } static i64 average(CoreHashMap* m, i64 (*f)(const CoreHashMap* 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) { CoreHashMap m; coreInitHashMap(&m, sizeof(int), sizeof(int)); fillOrder(&m); m.hasher = nullptr; 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) { CoreHashMap 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); } /*static void testProbing(int n) { Core::ProbingHashMap m; Core::ProbingHashMap m2; fillOrder(m); fillChaos(m2); Logger::log(Logger::COLOR_GRAY, "Order | Chaos"); Logger::log(Logger::COLOR_GRAY, "Search | # ms | # ms", average(m, testSearch, n), average(m2, testSearch, n)); Logger::log(Logger::COLOR_GRAY, "EmptySearch | # ms | # ms", average(m, testEmptySearch, n), average(m2, testEmptySearch, n)); }*/ int main() { //(void)testProbing; order(3); chaos(3); // testProbing(3); return 0; }