fork(3) download
  1. //
  2. // main.cpp
  3. // test_map
  4. //
  5. // Created by Sergey on 10.09.14.
  6. // Copyright (c) 2014 Sergey Perepelitsa. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <map>
  11. #include <unordered_map>
  12. #include <chrono>
  13.  
  14. enum class StringEnum
  15. {
  16. stringparam00,
  17. stringparam01,
  18. stringparam02,
  19. stringparam03,
  20. stringparam04,
  21. stringparam05,
  22. stringparam06,
  23. stringparam07,
  24. stringparam08,
  25. stringparam09,
  26. stringparam10,
  27. stringparam11,
  28. stringparam12,
  29. stringparam13,
  30. stringparam14,
  31. stringparam15,
  32. stringparam16,
  33. stringparam17,
  34. stringparam18,
  35. stringparam19
  36. };
  37.  
  38. int main(int argc, const char * argv[])
  39. {
  40.  
  41. std::map<std::string,int> string_map = {
  42. {"stringparam00",0},
  43. {"stringparam01",0},
  44. {"stringparam02",0},
  45. {"stringparam03",0},
  46. {"stringparam04",0},
  47. {"stringparam05",0},
  48. {"stringparam06",0},
  49. {"stringparam07",0},
  50. {"stringparam08",0},
  51. {"stringparam09",0},
  52. {"stringparam10",0},
  53. {"stringparam11",0},
  54. {"stringparam12",0},
  55. {"stringparam13",0},
  56. {"stringparam14",0},
  57. {"stringparam15",0},
  58. {"stringparam16",0},
  59. {"stringparam17",0},
  60. {"stringparam18",0},
  61. {"stringparam19",0}
  62. };
  63. std::unordered_map<std::string,int> unordered_string_map = {
  64. {"stringparam00",0},
  65. {"stringparam01",0},
  66. {"stringparam02",0},
  67. {"stringparam03",0},
  68. {"stringparam04",0},
  69. {"stringparam05",0},
  70. {"stringparam06",0},
  71. {"stringparam07",0},
  72. {"stringparam08",0},
  73. {"stringparam09",0},
  74. {"stringparam10",0},
  75. {"stringparam11",0},
  76. {"stringparam12",0},
  77. {"stringparam13",0},
  78. {"stringparam14",0},
  79. {"stringparam15",0},
  80. {"stringparam16",0},
  81. {"stringparam17",0},
  82. {"stringparam18",0},
  83. {"stringparam19",0}
  84. };
  85. std::map<StringEnum,int> enum_map = {
  86. {StringEnum::stringparam00,0},
  87. {StringEnum::stringparam01,0},
  88. {StringEnum::stringparam02,0},
  89. {StringEnum::stringparam03,0},
  90. {StringEnum::stringparam04,0},
  91. {StringEnum::stringparam05,0},
  92. {StringEnum::stringparam06,0},
  93. {StringEnum::stringparam07,0},
  94. {StringEnum::stringparam08,0},
  95. {StringEnum::stringparam09,0},
  96. {StringEnum::stringparam10,0},
  97. {StringEnum::stringparam11,0},
  98. {StringEnum::stringparam12,0},
  99. {StringEnum::stringparam13,0},
  100. {StringEnum::stringparam14,0},
  101. {StringEnum::stringparam15,0},
  102. {StringEnum::stringparam16,0},
  103. {StringEnum::stringparam17,0},
  104. {StringEnum::stringparam18,0},
  105. {StringEnum::stringparam19,0}
  106. };
  107.  
  108. int res = 0;
  109.  
  110. std::chrono::high_resolution_clock::time_point t = std::chrono::high_resolution_clock::now();
  111. for(int i=0;i<10000000;++i)
  112. {
  113. res += string_map.find("stringparam10")->second;
  114. }
  115. auto duration = std::chrono::high_resolution_clock::now() - t;
  116. std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() << std::endl;
  117.  
  118. t = std::chrono::high_resolution_clock::now();
  119. for(int i=0;i<10000000;++i)
  120. {
  121. res += unordered_string_map.find("stringparam10")->second;
  122. }
  123. duration = std::chrono::high_resolution_clock::now() - t;
  124. std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() << std::endl;
  125.  
  126. t = std::chrono::high_resolution_clock::now();
  127. for(int i=0;i<10000000;++i)
  128. {
  129. res += enum_map.find(StringEnum::stringparam10)->second;
  130. }
  131. duration = std::chrono::high_resolution_clock::now() - t;
  132. std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() << std::endl;
  133.  
  134. return 0;
  135. }
  136.  
  137.  
Success #stdin #stdout 3.9s 3480KB
stdin
Standard input is empty
stdout
1809
2014
83