handle_bench.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright 2010-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx/blob/master/LICENSE
  4. */
  5. #include <bx/timer.h>
  6. #include <bx/handlealloc.h>
  7. #include <tinystl/allocator.h>
  8. #include <tinystl/unordered_map.h>
  9. #include <unordered_map>
  10. #include <stdio.h>
  11. #include <assert.h>
  12. template<typename MapType>
  13. bool mapRemove(MapType& _map, const typename MapType::value_type::first_type& _first)
  14. {
  15. typename MapType::const_iterator it = _map.find(_first);
  16. if (it != _map.end() )
  17. {
  18. _map.erase(it);
  19. return true;
  20. }
  21. return false;
  22. }
  23. int main()
  24. {
  25. const uint32_t numElements = 4<<10;
  26. const uint32_t numIterations = 16;
  27. //
  28. {
  29. int64_t elapsed = -bx::getHPCounter();
  30. for (uint32_t ii = 0; ii < numIterations; ++ii)
  31. {
  32. typedef tinystl::unordered_map<uint64_t, uint16_t> TinyStlUnorderedMap;
  33. TinyStlUnorderedMap map;
  34. // map.reserve(numElements);
  35. for (uint32_t jj = 0; jj < numElements; ++jj)
  36. {
  37. tinystl::pair<TinyStlUnorderedMap::iterator, bool> ok = map.insert(tinystl::make_pair(uint64_t(jj), uint16_t(jj) ) );
  38. assert(ok.second); BX_UNUSED(ok);
  39. }
  40. for (uint32_t jj = 0; jj < numElements; ++jj)
  41. {
  42. bool ok = mapRemove(map, uint64_t(jj) );
  43. assert(ok); BX_UNUSED(ok);
  44. }
  45. assert(map.size() == 0);
  46. }
  47. elapsed += bx::getHPCounter();
  48. printf(" TinyStl: %15f\n", double(elapsed) );
  49. }
  50. ///
  51. {
  52. int64_t elapsed = -bx::getHPCounter();
  53. for (uint32_t ii = 0; ii < numIterations; ++ii)
  54. {
  55. typedef std::unordered_map<uint64_t, uint16_t> StdUnorderedMap;
  56. StdUnorderedMap map;
  57. map.reserve(numElements);
  58. for (uint32_t jj = 0; jj < numElements; ++jj)
  59. {
  60. std::pair<StdUnorderedMap::iterator, bool> ok = map.insert(std::make_pair(uint64_t(jj), uint16_t(jj) ) );
  61. assert(ok.second); BX_UNUSED(ok);
  62. }
  63. for (uint32_t jj = 0; jj < numElements; ++jj)
  64. {
  65. bool ok = mapRemove(map, uint64_t(jj) );
  66. assert(ok); BX_UNUSED(ok);
  67. }
  68. assert(map.size() == 0);
  69. }
  70. elapsed += bx::getHPCounter();
  71. printf(" STL: %15f\n", double(elapsed) );
  72. }
  73. ///
  74. {
  75. int64_t elapsed = -bx::getHPCounter();
  76. for (uint32_t ii = 0; ii < numIterations; ++ii)
  77. {
  78. typedef bx::HandleHashMapT<numElements+numElements/2, uint64_t> HandleHashMap;
  79. HandleHashMap map;
  80. for (uint32_t jj = 0; jj < numElements; ++jj)
  81. {
  82. bool ok = map.insert(jj, uint16_t(jj) );
  83. assert(ok); BX_UNUSED(ok);
  84. }
  85. for (uint32_t jj = 0; jj < numElements; ++jj)
  86. {
  87. bool ok = map.removeByKey(uint64_t(jj) );
  88. assert(ok); BX_UNUSED(ok);
  89. }
  90. assert(map.getNumElements() == 0);
  91. }
  92. elapsed += bx::getHPCounter();
  93. printf("HandleHashMap: %15f\n", double(elapsed) );
  94. }
  95. extern void simd_bench();
  96. simd_bench();
  97. extern void math_bench();
  98. math_bench();
  99. return bx::kExitSuccess;
  100. }