Kaynağa Gözat

Reactivate performance tests

Kajetan Johannes Hammerle 1 yıl önce
ebeveyn
işleme
090180a9a1
2 değiştirilmiş dosya ile 100 ekleme ve 4 silme
  1. 5 4
      CMakeLists.txt
  2. 95 0
      performance/Main.c

+ 5 - 4
CMakeLists.txt

@@ -46,8 +46,8 @@ set(SRC_TESTS
 )
 
 set(SRC_PERFORMANCE
-    #"performance/Main.cpp"
-    #"test/Test.cpp"
+    "performance/Main.c"
+    "test/Test.c"
 )
 
 if("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
@@ -125,5 +125,6 @@ add_executable(test ${SRC_TESTS})
 target_link_libraries(test PRIVATE core)
 target_compile_definitions(test PRIVATE ${DEFINITIONS})
 
-#add_executable(performance ${SRC_PERFORMANCE})
-#target_link_libraries(performance PRIVATE core)
+add_executable(performance ${SRC_PERFORMANCE})
+target_link_libraries(performance PRIVATE core)
+target_include_directories(performance PRIVATE include)

+ 95 - 0
performance/Main.c

@@ -0,0 +1,95 @@
+#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;
+}