You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
3.8 KiB

2 months ago
  1. // compile on linux with: g++ -std=c++11 -O2 perftest1.cc -o perftest1
  2. // -----------------------------------------------------------------------
  3. #include <fstream>
  4. #include <iostream>
  5. #include <ctime>
  6. #include <cstdio>
  7. #include <climits>
  8. #include <functional>
  9. #include <vector>
  10. #include <utility>
  11. #include <sparsepp/spp_timer.h>
  12. #define SPP 1
  13. #define DENSE 0
  14. #define SPARSE 0
  15. #define STD 0
  16. #if SPP
  17. #include <sparsepp/spp.h>
  18. #elif DENSE
  19. #include <google/dense_hash_map>
  20. #elif SPARSE
  21. #include <google/sparse_hash_map>
  22. #elif STD
  23. #include <unordered_map>
  24. #endif
  25. using std::make_pair;
  26. template <class T>
  27. void test(T &s, int count)
  28. {
  29. spp::Timer<std::milli> timer;
  30. timer.snap();
  31. srand(0);
  32. for (int i = 0; i < count; ++i)
  33. s.insert(make_pair(rand(), i));
  34. printf("%d random inserts in %5.2f seconds\n", count, timer.get_delta() / 1000);
  35. timer.snap();
  36. srand(0);
  37. for (int i = 0; i < count; ++i)
  38. s.find(rand());
  39. printf("%d random finds in %5.2f seconds\n", count, timer.get_delta() / 1000);
  40. timer.snap();
  41. srand(1);
  42. for (int i = 0; i < count; ++i)
  43. s.find(rand());
  44. printf("%d random not-finds in %5.2f seconds\n", count, timer.get_delta() / 1000);
  45. s.clear();
  46. timer.snap();
  47. srand(0);
  48. for (int i = 0; i < count; ++i)
  49. s.insert(make_pair(i, i));
  50. printf("%d sequential inserts in %5.2f seconds\n", count, timer.get_delta() / 1000);
  51. timer.snap();
  52. srand(0);
  53. for (int i = 0; i < count; ++i)
  54. s.find(i);
  55. printf("%d sequential finds in %5.2f seconds\n", count, timer.get_delta() / 1000);
  56. timer.snap();
  57. srand(1);
  58. for (int i = 0; i < count; ++i)
  59. {
  60. int x = rand();
  61. s.find(x);
  62. }
  63. printf("%d random not-finds in %5.2f seconds\n", count, timer.get_delta() / 1000);
  64. s.clear();
  65. timer.snap();
  66. srand(0);
  67. for (int i = 0; i < count; ++i)
  68. s.insert(make_pair(-i, -i));
  69. printf("%d neg sequential inserts in %5.2f seconds\n", count, timer.get_delta() / 1000);
  70. timer.snap();
  71. srand(0);
  72. for (int i = 0; i < count; ++i)
  73. s.find(-i);
  74. printf("%d neg sequential finds in %5.2f seconds\n", count, timer.get_delta() / 1000);
  75. timer.snap();
  76. srand(1);
  77. for (int i = 0; i < count; ++i)
  78. s.find(rand());
  79. printf("%d random not-finds in %5.2f seconds\n", count, timer.get_delta() / 1000);
  80. s.clear();
  81. }
  82. struct Hasher64 {
  83. size_t operator()(uint64_t k) const { return (k ^ 14695981039346656037ULL) * 1099511628211ULL; }
  84. };
  85. struct Hasher32 {
  86. size_t operator()(uint32_t k) const { return (k ^ 2166136261U) * 16777619UL; }
  87. };
  88. struct Hasheri32 {
  89. size_t operator()(int k) const
  90. {
  91. return (k ^ 2166136261U) * 16777619UL;
  92. }
  93. };
  94. struct Hasher_32 {
  95. size_t operator()(int k) const
  96. {
  97. uint32_t a = (uint32_t)k;
  98. #if 0
  99. a = (a ^ 61) ^ (a >> 16);
  100. a = a + (a << 3);
  101. a = a ^ (a >> 4);
  102. a = a * 0x27d4eb2d;
  103. a = a ^ (a >> 15);
  104. return a;
  105. #else
  106. a = a ^ (a >> 4);
  107. a = (a ^ 0xdeadbeef) + (a << 5);
  108. a = a ^ (a >> 11);
  109. return a;
  110. #endif
  111. }
  112. };
  113. int main()
  114. {
  115. #if SPP
  116. spp::sparse_hash_map<int, int /*, Hasheri32 */> s;
  117. printf ("Testing spp::sparse_hash_map\n");
  118. #elif DENSE
  119. google::dense_hash_map<int, int/* , Hasher_32 */> s;
  120. s.set_empty_key(-INT_MAX);
  121. s.set_deleted_key(-(INT_MAX - 1));
  122. printf ("Testing google::dense_hash_map\n");
  123. #elif SPARSE
  124. google::sparse_hash_map<int, int/* , Hasher_32 */> s;
  125. s.set_deleted_key(-INT_MAX);
  126. printf ("Testing google::sparse_hash_map\n");
  127. #elif STD
  128. std::unordered_map<int, int/* , Hasher_32 */> s;
  129. printf ("Testing std::unordered_map\n");
  130. #endif
  131. printf ("------------------------------\n");
  132. test(s, 50000000);
  133. return 0;
  134. }