map_assign_perf.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright (C) 2016-2018 T. Zachary Laine
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/yap/expression.hpp>
  7. #include <boost/assign/list_of.hpp>
  8. #include <map>
  9. #include <iostream>
  10. #include <benchmark/benchmark.h>
  11. template<typename Key, typename Value, typename Allocator>
  12. struct map_list_of_transform
  13. {
  14. template<typename Fn, typename Key2, typename Value2>
  15. auto operator()(
  16. boost::yap::expr_tag<boost::yap::expr_kind::call>,
  17. Fn const & fn,
  18. Key2 && key,
  19. Value2 && value)
  20. {
  21. boost::yap::transform(
  22. boost::yap::as_expr<boost::yap::minimal_expr>(fn), *this);
  23. map.emplace(
  24. Key{std::forward<Key2 &&>(key)},
  25. Value{std::forward<Value2 &&>(value)});
  26. return 0;
  27. }
  28. std::map<Key, Value, Allocator> map;
  29. };
  30. template<boost::yap::expr_kind Kind, typename Tuple>
  31. struct map_list_of_expr
  32. {
  33. static boost::yap::expr_kind const kind = Kind;
  34. Tuple elements;
  35. template<typename Key, typename Value, typename Allocator>
  36. operator std::map<Key, Value, Allocator>() const
  37. {
  38. map_list_of_transform<Key, Value, Allocator> transform;
  39. boost::yap::transform(*this, transform);
  40. return transform.map;
  41. }
  42. BOOST_YAP_USER_CALL_OPERATOR(::map_list_of_expr)
  43. };
  44. struct map_list_of_tag
  45. {};
  46. auto map_list_of =
  47. boost::yap::make_terminal<map_list_of_expr>(map_list_of_tag{});
  48. std::map<std::string, int> make_map_with_boost_yap()
  49. {
  50. return map_list_of("<", 1)("<=", 2)(">", 3)(">=", 4)("=", 5)("<>", 6);
  51. }
  52. void BM_make_map_with_boost_yap(benchmark::State & state)
  53. {
  54. int i = 0;
  55. while (state.KeepRunning()) {
  56. {
  57. std::map<std::string, int> map = make_map_with_boost_yap();
  58. state.PauseTiming();
  59. for (auto && x : map) {
  60. i += x.second;
  61. }
  62. }
  63. state.ResumeTiming();
  64. }
  65. std::cout << "Sum of ints in all maps made=" << i << "\n";
  66. }
  67. std::map<std::string, int> make_map_with_boost_assign()
  68. {
  69. return boost::assign::map_list_of("<", 1)("<=", 2)(">", 3)(">=", 4)("=", 5)(
  70. "<>", 6);
  71. }
  72. void BM_make_map_with_boost_assign(benchmark::State & state)
  73. {
  74. int i = 0;
  75. while (state.KeepRunning()) {
  76. {
  77. std::map<std::string, int> map = make_map_with_boost_assign();
  78. state.PauseTiming();
  79. for (auto && x : map) {
  80. i += x.second;
  81. }
  82. }
  83. state.ResumeTiming();
  84. }
  85. std::cout << "Sum of ints in all maps made=" << i << "\n";
  86. }
  87. std::map<std::string, int> make_map_manually()
  88. {
  89. std::map<std::string, int> retval;
  90. retval.emplace("<", 1);
  91. retval.emplace("<=", 2);
  92. retval.emplace(">", 3);
  93. retval.emplace(">=", 4);
  94. retval.emplace("=", 5);
  95. retval.emplace("<>", 6);
  96. return retval;
  97. }
  98. void BM_make_map_manually(benchmark::State & state)
  99. {
  100. int i = 0;
  101. while (state.KeepRunning()) {
  102. {
  103. std::map<std::string, int> map = make_map_manually();
  104. state.PauseTiming();
  105. for (auto && x : map) {
  106. i += x.second;
  107. }
  108. }
  109. state.ResumeTiming();
  110. }
  111. std::cout << "Sum of ints in all maps made=" << i << "\n";
  112. }
  113. std::map<std::string, int> make_map_inializer_list()
  114. {
  115. std::map<std::string, int> retval = {
  116. {"<", 1}, {"<=", 2}, {">", 3}, {">=", 4}, {"=", 5}, {"<>", 6}};
  117. return retval;
  118. }
  119. void BM_make_map_inializer_list(benchmark::State & state)
  120. {
  121. int i = 0;
  122. while (state.KeepRunning()) {
  123. {
  124. std::map<std::string, int> map = make_map_inializer_list();
  125. state.PauseTiming();
  126. for (auto && x : map) {
  127. i += x.second;
  128. }
  129. }
  130. state.ResumeTiming();
  131. }
  132. std::cout << "Sum of ints in all maps made=" << i << "\n";
  133. }
  134. BENCHMARK(BM_make_map_with_boost_yap);
  135. BENCHMARK(BM_make_map_with_boost_assign);
  136. BENCHMARK(BM_make_map_manually);
  137. BENCHMARK(BM_make_map_inializer_list);
  138. BENCHMARK_MAIN()