Main.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <stdio.h>
  2. #include "core/HashMap.h"
  3. #include "core/Random.h"
  4. #include "core/Utility.h"
  5. HASHMAP(int, int, Int)
  6. HASHMAP_SOURCE(int, int, Int)
  7. static i64 testSearch(const HashMapInt* m) {
  8. i64 nanos = getNanos();
  9. volatile int sum = 0;
  10. for(int i = 0; i < 10000; i++) {
  11. for(int k = -5000; k < 5000; k++) {
  12. const int* s = searchHashMapKeyInt(m, i + k);
  13. if(s != nullptr) {
  14. sum = sum + *s;
  15. }
  16. }
  17. }
  18. return getNanos() - nanos;
  19. }
  20. static i64 testEmptySearch(const HashMapInt* m) {
  21. i64 nanos = getNanos();
  22. volatile int sum = 0;
  23. for(int i = 0; i < 100'000'000; i++) {
  24. const int* s = searchHashMapKeyInt(m, -i);
  25. if(s != nullptr) {
  26. sum = sum + *s;
  27. }
  28. }
  29. return getNanos() - nanos;
  30. }
  31. static void fillOrder(HashMapInt* m) {
  32. i64 nanos = getNanos();
  33. for(int i = 0; i < 10000; i++) {
  34. *putHashMapKeyInt(m, i) = i * i;
  35. }
  36. printf("Fill Order: %ldns\n", getNanos() - nanos);
  37. }
  38. static void fillChaos(HashMapInt* m) {
  39. i64 nanos = getNanos();
  40. Random random;
  41. initRandom(&random, 0);
  42. for(int i = 0; i < 10000; i++) {
  43. int r = randomI32(&random, 0, 10000);
  44. *putHashMapKeyInt(m, r) = r * r;
  45. }
  46. printf("Fill Chaos: %ldns\n", getNanos() - nanos);
  47. }
  48. static i64 average(HashMapInt* m, i64 (*f)(const HashMapInt* m), int n) {
  49. i64 sum = 0;
  50. for(int i = 0; i < n; i++) {
  51. sum += f(m);
  52. }
  53. return (i64)(sum / (n * 1'000'000));
  54. }
  55. static void order(int n) {
  56. HashMapInt m;
  57. initHashMapInt(&m);
  58. fillOrder(&m);
  59. puts("Order Probing");
  60. printf("Search | %ld ms\n", average(&m, testSearch, n));
  61. printf("EmptySearch | %ld ms\n", average(&m, testEmptySearch, n));
  62. destroyHashMapInt(&m);
  63. }
  64. static void chaos(int n) {
  65. HashMapInt m;
  66. initHashMapInt(&m);
  67. fillChaos(&m);
  68. puts("Chaos Probing");
  69. printf("Search | %ld ms\n", average(&m, testSearch, n));
  70. printf("EmptySearch | %ld ms\n", average(&m, testEmptySearch, n));
  71. destroyHashMapInt(&m);
  72. }
  73. int main() {
  74. order(3);
  75. chaos(3);
  76. return 0;
  77. }