Main.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 = coreNanos();
  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 coreNanos() - nanos;
  17. }
  18. static i64 testEmptySearch(const CoreHashMap* m) {
  19. i64 nanos = coreNanos();
  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 coreNanos() - nanos;
  28. }
  29. static void fillOrder(CoreHashMap* m) {
  30. for(int i = 0; i < 10000; i++) {
  31. coreHashMapPut(m, int, i, int, i* i);
  32. }
  33. }
  34. static void fillChaos(CoreHashMap* m) {
  35. CoreRandom random;
  36. coreInitRandom(&random, 0);
  37. for(int i = 0; i < 10000; i++) {
  38. int r = coreRandomI32(&random, 0, 10000);
  39. coreHashMapPut(m, int, r, int, r* r);
  40. }
  41. }
  42. static i64 average(CoreHashMap* m, i64 (*f)(const CoreHashMap* m), int n) {
  43. i64 sum = 0;
  44. for(int i = 0; i < n; i++) {
  45. sum += f(m);
  46. }
  47. return (i64)(sum / (n * 1'000'000));
  48. }
  49. static void order(int n) {
  50. CoreHashMap m =
  51. CORE_HASH_MAP(sizeof(int), sizeof(int), coreHash, coreEqual);
  52. fillOrder(&m);
  53. puts("Order Probing");
  54. printf("Search | %ld ms\n", average(&m, testSearch, n));
  55. printf("EmptySearch | %ld ms\n", average(&m, testEmptySearch, n));
  56. coreDestroyHashMap(&m);
  57. }
  58. static void chaos(int n) {
  59. CoreHashMap m =
  60. CORE_HASH_MAP(sizeof(int), sizeof(int), coreHash, coreEqual);
  61. fillChaos(&m);
  62. puts("Chaos Probing");
  63. printf("Search | %ld ms\n", average(&m, testSearch, n));
  64. printf("EmptySearch | %ld ms\n", average(&m, testEmptySearch, n));
  65. coreDestroyHashMap(&m);
  66. }
  67. /*static void testProbing(int n) {
  68. Core::ProbingHashMap<int, int> m;
  69. Core::ProbingHashMap<int, int> m2;
  70. fillOrder(m);
  71. fillChaos(m2);
  72. Logger::log(Logger::COLOR_GRAY, "Order | Chaos");
  73. Logger::log(Logger::COLOR_GRAY, "Search | # ms | # ms",
  74. average(m, testSearch, n), average(m2, testSearch, n));
  75. Logger::log(Logger::COLOR_GRAY, "EmptySearch | # ms | # ms",
  76. average(m, testEmptySearch, n),
  77. average(m2, testEmptySearch, n));
  78. }*/
  79. int main() {
  80. //(void)testProbing;
  81. order(3);
  82. chaos(3);
  83. // testProbing(3);
  84. return 0;
  85. }