HashMap.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <Tests/Framework/Framework.h>
  6. #include <Tests/Util/Foo.h>
  7. #include "anki/util/HashMap.h"
  8. #include "anki/util/DynamicArray.h"
  9. #include "anki/util/HighRezTimer.h"
  10. #include <unordered_map>
  11. #include <algorithm>
  12. using namespace anki;
  13. class Hasher
  14. {
  15. public:
  16. U64 operator()(int x)
  17. {
  18. return x;
  19. }
  20. };
  21. ANKI_TEST(Util, HashMap)
  22. {
  23. HeapAllocator<U8> alloc(allocAligned, nullptr);
  24. int vals[] = {20, 15, 5, 1, 10, 0, 18, 6, 7, 11, 13, 3};
  25. U valsSize = sizeof(vals) / sizeof(vals[0]);
  26. // Simple
  27. {
  28. HashMap<int, int, Hasher> map;
  29. map.emplace(alloc, 20, 1);
  30. map.emplace(alloc, 21, 1);
  31. map.destroy(alloc);
  32. }
  33. // Add more and iterate
  34. {
  35. HashMap<int, int, Hasher> map;
  36. for(U i = 0; i < valsSize; ++i)
  37. {
  38. map.emplace(alloc, vals[i], vals[i] * 10);
  39. }
  40. U count = 0;
  41. auto it = map.getBegin();
  42. for(; it != map.getEnd(); ++it)
  43. {
  44. ++count;
  45. }
  46. ANKI_TEST_EXPECT_EQ(count, valsSize);
  47. map.destroy(alloc);
  48. }
  49. // Erase
  50. {
  51. HashMap<int, int, Hasher> map;
  52. for(U i = 0; i < valsSize; ++i)
  53. {
  54. map.emplace(alloc, vals[i], vals[i] * 10);
  55. }
  56. for(U i = valsSize - 1; i != 0; --i)
  57. {
  58. HashMap<int, int, Hasher>::Iterator it = map.find(vals[i]);
  59. ANKI_TEST_EXPECT_NEQ(it, map.getEnd());
  60. map.erase(alloc, it);
  61. map.emplace(alloc, vals[i], vals[i] * 10);
  62. }
  63. map.destroy(alloc);
  64. }
  65. // Find
  66. {
  67. HashMap<int, int, Hasher> map;
  68. for(U i = 0; i < valsSize; ++i)
  69. {
  70. map.emplace(alloc, vals[i], vals[i] * 10);
  71. }
  72. for(U i = valsSize - 1; i != 0; --i)
  73. {
  74. HashMap<int, int, Hasher>::Iterator it = map.find(vals[i]);
  75. ANKI_TEST_EXPECT_NEQ(it, map.getEnd());
  76. ANKI_TEST_EXPECT_EQ(*it, vals[i] * 10);
  77. }
  78. map.destroy(alloc);
  79. }
  80. // Fuzzy test
  81. {
  82. const U MAX = 1000;
  83. HashMap<int, int, Hasher> akMap;
  84. std::vector<int> numbers;
  85. // Insert random
  86. for(U i = 0; i < MAX; ++i)
  87. {
  88. I32 num;
  89. while(1)
  90. {
  91. num = rand();
  92. if(std::find(numbers.begin(), numbers.end(), num) == numbers.end())
  93. {
  94. // Not found
  95. ANKI_TEST_EXPECT_EQ(akMap.find(num), akMap.getEnd());
  96. akMap.emplace(alloc, num, num);
  97. numbers.push_back(num);
  98. break;
  99. }
  100. else
  101. {
  102. // Found
  103. ANKI_TEST_EXPECT_NEQ(akMap.find(num), akMap.getEnd());
  104. }
  105. }
  106. }
  107. // Remove randomly
  108. U count = MAX;
  109. while(count--)
  110. {
  111. U idx = rand() % (count + 1);
  112. int num = numbers[idx];
  113. numbers.erase(numbers.begin() + idx);
  114. auto it = akMap.find(num);
  115. akMap.erase(alloc, it);
  116. }
  117. akMap.destroy(alloc);
  118. }
  119. // Bench it
  120. {
  121. using AkMap = HashMap<int, int, Hasher>;
  122. AkMap akMap(128, 32, 0.9f);
  123. using StlMap =
  124. std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, HeapAllocator<std::pair<const int, int>>>;
  125. StlMap stdMap(10, std::hash<int>(), std::equal_to<int>(), alloc);
  126. std::unordered_map<int, int> tmpMap;
  127. HighRezTimer timer;
  128. // Create a huge set
  129. const U32 COUNT = 1024 * 1024 * 10;
  130. DynamicArrayAuto<int> vals(alloc);
  131. vals.create(COUNT);
  132. for(U32 i = 0; i < COUNT; ++i)
  133. {
  134. // Put unique keys
  135. int v;
  136. do
  137. {
  138. v = rand();
  139. } while(tmpMap.find(v) != tmpMap.end());
  140. tmpMap[v] = 1;
  141. vals[i] = v;
  142. }
  143. // Insertion
  144. {
  145. // Put the vals AnKi
  146. timer.start();
  147. for(U32 i = 0; i < COUNT; ++i)
  148. {
  149. akMap.emplace(alloc, vals[i], vals[i]);
  150. }
  151. timer.stop();
  152. Second akTime = timer.getElapsedTime();
  153. // Put the vals STL
  154. timer.start();
  155. for(U32 i = 0; i < COUNT; ++i)
  156. {
  157. stdMap[vals[i]] = vals[i];
  158. }
  159. timer.stop();
  160. Second stlTime = timer.getElapsedTime();
  161. ANKI_TEST_LOGI("Inserting bench: STL %f AnKi %f | %f%%", stlTime, akTime, stlTime / akTime * 100.0);
  162. }
  163. // Search
  164. {
  165. I64 count = 0; // To avoid compiler opts
  166. // Find values AnKi
  167. timer.start();
  168. for(U32 i = 0; i < COUNT; ++i)
  169. {
  170. auto it = akMap.find(vals[i]);
  171. count += *it;
  172. }
  173. timer.stop();
  174. Second akTime = timer.getElapsedTime();
  175. // Find values STL
  176. timer.start();
  177. for(U32 i = 0; i < COUNT; ++i)
  178. {
  179. count += stdMap[vals[i]];
  180. }
  181. timer.stop();
  182. Second stlTime = timer.getElapsedTime();
  183. ANKI_TEST_LOGI("Find bench: STL %f AnKi %f | %f%% (%ld)", stlTime, akTime, stlTime / akTime * 100.0, count);
  184. }
  185. // Delete
  186. {
  187. // Remove in random order
  188. std::random_shuffle(vals.begin(), vals.end());
  189. // Random delete AnKi
  190. Second akTime = 0.0;
  191. for(U32 i = 0; i < vals.getSize(); ++i)
  192. {
  193. auto it = akMap.find(vals[i]);
  194. timer.start();
  195. akMap.erase(alloc, it);
  196. timer.stop();
  197. akTime += timer.getElapsedTime();
  198. }
  199. // Random delete STL
  200. Second stlTime = 0.0;
  201. for(U32 i = 0; i < vals.getSize(); ++i)
  202. {
  203. auto it = stdMap.find(vals[i]);
  204. timer.start();
  205. stdMap.erase(it);
  206. timer.stop();
  207. stlTime += timer.getElapsedTime();
  208. }
  209. ANKI_TEST_LOGI("Deleting bench: STL %f AnKi %f | %f%%", stlTime, akTime, stlTime / akTime * 100.0);
  210. }
  211. akMap.destroy(alloc);
  212. }
  213. }