Main.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "core/Clock.hpp"
  2. #include "core/HashMap.hpp"
  3. #include "core/Logger.hpp"
  4. #include "core/Random.hpp"
  5. using HashMapInt = Core::HashMap<int, int>;
  6. using Core::Clock;
  7. static i64 testSearch(const HashMapInt& m) {
  8. i64 nanos = Clock::getNanos();
  9. volatile int sum = 0;
  10. for(int i = 0; i < 10'000; i++) {
  11. for(int k = -5000; k < 5000; k++) {
  12. const int* s = m.search(i + k);
  13. if(s != nullptr) {
  14. sum = sum + *s;
  15. }
  16. }
  17. }
  18. return Clock::getNanos() - nanos;
  19. }
  20. static i64 testEmptySearch(const HashMapInt& m) {
  21. i64 nanos = Clock::getNanos();
  22. volatile int sum = 0;
  23. for(int i = 0; i < 100'000'000; i++) {
  24. const int* s = m.search(-i);
  25. if(s != nullptr) {
  26. sum = sum + *s;
  27. }
  28. }
  29. return Clock::getNanos() - nanos;
  30. }
  31. static void fillOrder(HashMapInt& m) {
  32. i64 nanos = Clock::getNanos();
  33. for(int i = 0; i < 10'000; i++) {
  34. m.put(i, i * i);
  35. }
  36. LOG_INFO("Fill Order: #ns", Clock::getNanos() - nanos);
  37. }
  38. static void fillChaos(HashMapInt& m) {
  39. i64 nanos = Clock::getNanos();
  40. Core::Random random(0);
  41. for(int i = 0; i < 10'000; i++) {
  42. int r = random.nextI32(0, 10'000);
  43. m.put(r, r * r);
  44. }
  45. LOG_INFO("Fill Chaos: #ns", Clock::getNanos() - nanos);
  46. }
  47. static i64 average(HashMapInt& m, i64 (*f)(const HashMapInt& m), int n) {
  48. i64 sum = 0;
  49. for(int i = 0; i < n; i++) {
  50. sum += f(m);
  51. }
  52. return static_cast<i64>(sum / (n * 1'000'000));
  53. }
  54. static void order(int n) {
  55. HashMapInt m;
  56. fillOrder(m);
  57. LOG_INFO("Order Probing");
  58. LOG_INFO("Search | #ms", average(m, testSearch, n));
  59. LOG_INFO("EmptySearch | #ms", average(m, testEmptySearch, n));
  60. }
  61. static void chaos(int n) {
  62. HashMapInt m;
  63. fillChaos(m);
  64. LOG_INFO("Chaos Probing");
  65. LOG_INFO("Search | #ms", average(m, testSearch, n));
  66. LOG_INFO("EmptySearch | #ms", average(m, testEmptySearch, n));
  67. }
  68. int main() {
  69. order(3);
  70. chaos(3);
  71. return 0;
  72. }