Main.c 2.2 KB

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