Main.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include <stdio.h>
  2. #include "core/HashMap.h"
  3. #include "core/Random.h"
  4. #include "core/Utility.h"
  5. static i64 testSearch(const CoreHashMap* m) {
  6. i64 nanos = coreGetNanos();
  7. volatile int sum = 0;
  8. for(int i = 0; i < 10000; i++) {
  9. for(int k = -5000; k < 5000; k++) {
  10. const int* s = coreHashMapSearchC(m, int, i + k, int);
  11. if(s != nullptr) {
  12. sum = sum + *s;
  13. }
  14. }
  15. }
  16. return coreGetNanos() - nanos;
  17. }
  18. static i64 testEmptySearch(const CoreHashMap* m) {
  19. i64 nanos = coreGetNanos();
  20. volatile int sum = 0;
  21. for(int i = 0; i < 100'000'000; i++) {
  22. const int* s = coreHashMapSearchC(m, int, -i, int);
  23. if(s != nullptr) {
  24. sum = sum + *s;
  25. }
  26. }
  27. return coreGetNanos() - nanos;
  28. }
  29. static void fillOrder(CoreHashMap* m) {
  30. i64 nanos = coreGetNanos();
  31. for(int i = 0; i < 10000; i++) {
  32. coreHashMapPut(m, int, i, int, i* i);
  33. }
  34. printf("Fill Order: %ldns\n", coreGetNanos() - nanos);
  35. }
  36. static void fillChaos(CoreHashMap* m) {
  37. i64 nanos = coreGetNanos();
  38. CoreRandom random;
  39. coreInitRandom(&random, 0);
  40. for(int i = 0; i < 10000; i++) {
  41. int r = coreRandomI32(&random, 0, 10000);
  42. coreHashMapPut(m, int, r, int, r* r);
  43. }
  44. printf("Fill Chaos: %ldns\n", coreGetNanos() - nanos);
  45. }
  46. static i64 average(CoreHashMap* m, i64 (*f)(const CoreHashMap* m), int n) {
  47. i64 sum = 0;
  48. for(int i = 0; i < n; i++) {
  49. sum += f(m);
  50. }
  51. return (i64)(sum / (n * 1'000'000));
  52. }
  53. static void order(int n) {
  54. CoreHashMap m;
  55. coreInitHashMap(&m, sizeof(int), sizeof(int));
  56. fillOrder(&m);
  57. m.hasher = nullptr;
  58. puts("Order Probing");
  59. printf("Search | %ld ms\n", average(&m, testSearch, n));
  60. printf("EmptySearch | %ld ms\n", average(&m, testEmptySearch, n));
  61. coreDestroyHashMap(&m);
  62. }
  63. static void chaos(int n) {
  64. CoreHashMap m;
  65. coreInitHashMap(&m, sizeof(int), sizeof(int));
  66. fillChaos(&m);
  67. puts("Chaos Probing");
  68. printf("Search | %ld ms\n", average(&m, testSearch, n));
  69. printf("EmptySearch | %ld ms\n", average(&m, testEmptySearch, n));
  70. coreDestroyHashMap(&m);
  71. }
  72. /*static void testProbing(int n) {
  73. Core::ProbingHashMap<int, int> m;
  74. Core::ProbingHashMap<int, int> m2;
  75. fillOrder(m);
  76. fillChaos(m2);
  77. Logger::log(Logger::COLOR_GRAY, "Order | Chaos");
  78. Logger::log(Logger::COLOR_GRAY, "Search | # ms | # ms",
  79. average(m, testSearch, n), average(m2, testSearch, n));
  80. Logger::log(Logger::COLOR_GRAY, "EmptySearch | # ms | # ms",
  81. average(m, testEmptySearch, n),
  82. average(m2, testEmptySearch, n));
  83. }*/
  84. int main() {
  85. //(void)testProbing;
  86. order(3);
  87. chaos(3);
  88. // testProbing(3);
  89. return 0;
  90. }