fork download
  1.  
  2. #include <array>
  3. #include <cstdint>
  4. #include <iostream>
  5. #include <map>
  6. #include <sys/time.h>
  7.  
  8.  
  9. double GetCurrentTime()
  10. {
  11. timeval tv;
  12. gettimeofday(&tv, NULL);
  13. return double (tv.tv_sec) + 0.000001 * tv.tv_usec;
  14. }
  15.  
  16.  
  17. double GetCurrentTimeMs()
  18. {
  19. return 1000 * GetCurrentTime();
  20. }
  21.  
  22.  
  23. struct Key1 {
  24. Key1(const std::array<uint8_t, 6> & inMAC, uint16_t inEtherType) :
  25. mMAC(inMAC),
  26. mEtherType(inEtherType)
  27. {
  28. }
  29.  
  30. union {
  31. struct {
  32. std::array<uint8_t, 6> mMAC;
  33. uint16_t mEtherType;
  34. };
  35. uint64_t mValue;
  36. };
  37.  
  38. friend bool operator==(const Key1 & lhs, const Key1 & rhs)
  39. { return lhs.mValue == rhs.mValue; }
  40.  
  41. friend bool operator<(const Key1 & lhs, const Key1 & rhs)
  42. { return lhs.mValue < rhs.mValue; }
  43. };
  44.  
  45.  
  46. typedef std::pair< std::array<uint8_t, 6>, uint16_t> Key2;
  47. typedef std::array<uint8_t, 8> Key3;
  48.  
  49.  
  50. //
  51. // Key 1
  52. //
  53. Key1 key1(std::array<uint8_t, 6>(), 0);
  54. void fill(std::map<Key1, int> & mapping)
  55. {
  56. mapping.insert(std::make_pair(key1, 0));
  57. }
  58.  
  59. void find(std::map<Key1, int> & mapping)
  60. {
  61. mapping.find(key1);
  62. }
  63.  
  64.  
  65. //
  66. // Key 2
  67. //
  68. Key2 key2 = Key2();
  69. void fill(std::map<Key2, int> & mapping)
  70. {
  71. mapping.insert(std::make_pair(key2, 0));
  72. }
  73.  
  74. void find(std::map<Key2, int> & mapping)
  75. {
  76. mapping.find(key2);
  77. }
  78.  
  79.  
  80. //
  81. // Key 3
  82. //
  83. Key3 key3 = Key3();
  84. void fill(std::map<Key3, int> & mapping)
  85. {
  86. mapping.insert(std::make_pair(key3, 0));
  87. }
  88.  
  89. void find(std::map<Key3, int> & mapping)
  90. {
  91. mapping.find(key3);
  92. }
  93.  
  94.  
  95. template<typename Key>
  96. void test()
  97. {
  98. std::map<Key, int> mapping;
  99. fill(mapping);
  100.  
  101. auto begin = GetCurrentTime();
  102. unsigned count = 0;
  103. while (GetCurrentTime() - begin <= 1.0)
  104. {
  105. for (unsigned i = 0; i < 100000; ++i)
  106. {
  107. find(mapping);
  108. count++;
  109. }
  110. }
  111.  
  112. std::cout << "Count: " << (count / 1000000) << "M in around 1 second." << std::endl;
  113. }
  114.  
  115.  
  116. int main()
  117. {
  118. test<Key1>();
  119. test<Key2>();
  120. test<Key3>();
  121. }
  122.  
Success #stdin #stdout 2.99s 2968KB
stdin
Standard input is empty
stdout
Count: 71M in around 1 second.
Count: 12M in around 1 second.
Count: 21M in around 1 second.