Main.cpp 2.0 KB

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