1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #include <stdio.h>
- #include "core/HashMap.h"
- #include "core/Random.h"
- #include "core/Utility.h"
- static i64 testSearch(const CoreHashMap* m) {
- i64 nanos = coreNanos();
- 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 coreNanos() - nanos;
- }
- static i64 testEmptySearch(const CoreHashMap* m) {
- i64 nanos = coreNanos();
- 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 coreNanos() - nanos;
- }
- static void fillOrder(CoreHashMap* m) {
- for(int i = 0; i < 10000; i++) {
- coreHashMapPut(m, int, i, int, i* i);
- }
- }
- static void fillChaos(CoreHashMap* m) {
- 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);
- }
- }
- 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 =
- CORE_HASH_MAP(sizeof(int), sizeof(int), coreHash, coreEqual);
- 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) {
- CoreHashMap m =
- CORE_HASH_MAP(sizeof(int), sizeof(int), coreHash, coreEqual);
- 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<int, int> m;
- Core::ProbingHashMap<int, int> 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;
- }
|