Main.c 2.2 KB

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