• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  
8  #include <boost/assign/list_of.hpp>
9  
10  #include <map>
11  #include <iostream>
12  
13  #include <benchmark/benchmark.h>
14  
15  
16  template<typename Key, typename Value, typename Allocator>
17  struct map_list_of_transform
18  {
19      template<typename Fn, typename Key2, typename Value2>
operator ()map_list_of_transform20      auto operator()(
21          boost::yap::expr_tag<boost::yap::expr_kind::call>,
22          Fn const & fn,
23          Key2 && key,
24          Value2 && value)
25      {
26          boost::yap::transform(
27              boost::yap::as_expr<boost::yap::minimal_expr>(fn), *this);
28          map.emplace(
29              Key{std::forward<Key2 &&>(key)},
30              Value{std::forward<Value2 &&>(value)});
31          return 0;
32      }
33  
34      std::map<Key, Value, Allocator> map;
35  };
36  
37  
38  template<boost::yap::expr_kind Kind, typename Tuple>
39  struct map_list_of_expr
40  {
41      static boost::yap::expr_kind const kind = Kind;
42  
43      Tuple elements;
44  
45      template<typename Key, typename Value, typename Allocator>
operator std::map<Key,Value,Allocator>map_list_of_expr46      operator std::map<Key, Value, Allocator>() const
47      {
48          map_list_of_transform<Key, Value, Allocator> transform;
49          boost::yap::transform(*this, transform);
50          return transform.map;
51      }
52  
53      BOOST_YAP_USER_CALL_OPERATOR(::map_list_of_expr)
54  };
55  
56  struct map_list_of_tag
57  {};
58  
59  auto map_list_of =
60      boost::yap::make_terminal<map_list_of_expr>(map_list_of_tag{});
61  
62  
make_map_with_boost_yap()63  std::map<std::string, int> make_map_with_boost_yap()
64  {
65      return map_list_of("<", 1)("<=", 2)(">", 3)(">=", 4)("=", 5)("<>", 6);
66  }
67  
BM_make_map_with_boost_yap(benchmark::State & state)68  void BM_make_map_with_boost_yap(benchmark::State & state)
69  {
70      int i = 0;
71      while (state.KeepRunning()) {
72          {
73              std::map<std::string, int> map = make_map_with_boost_yap();
74              state.PauseTiming();
75              for (auto && x : map) {
76                  i += x.second;
77              }
78          }
79          state.ResumeTiming();
80      }
81      std::cout << "Sum of ints in all maps made=" << i << "\n";
82  }
83  
make_map_with_boost_assign()84  std::map<std::string, int> make_map_with_boost_assign()
85  {
86      return boost::assign::map_list_of("<", 1)("<=", 2)(">", 3)(">=", 4)("=", 5)(
87          "<>", 6);
88  }
89  
BM_make_map_with_boost_assign(benchmark::State & state)90  void BM_make_map_with_boost_assign(benchmark::State & state)
91  {
92      int i = 0;
93      while (state.KeepRunning()) {
94          {
95              std::map<std::string, int> map = make_map_with_boost_assign();
96              state.PauseTiming();
97              for (auto && x : map) {
98                  i += x.second;
99              }
100          }
101          state.ResumeTiming();
102      }
103      std::cout << "Sum of ints in all maps made=" << i << "\n";
104  }
105  
make_map_manually()106  std::map<std::string, int> make_map_manually()
107  {
108      std::map<std::string, int> retval;
109      retval.emplace("<", 1);
110      retval.emplace("<=", 2);
111      retval.emplace(">", 3);
112      retval.emplace(">=", 4);
113      retval.emplace("=", 5);
114      retval.emplace("<>", 6);
115      return retval;
116  }
117  
BM_make_map_manually(benchmark::State & state)118  void BM_make_map_manually(benchmark::State & state)
119  {
120      int i = 0;
121      while (state.KeepRunning()) {
122          {
123              std::map<std::string, int> map = make_map_manually();
124              state.PauseTiming();
125              for (auto && x : map) {
126                  i += x.second;
127              }
128          }
129          state.ResumeTiming();
130      }
131      std::cout << "Sum of ints in all maps made=" << i << "\n";
132  }
133  
make_map_inializer_list()134  std::map<std::string, int> make_map_inializer_list()
135  {
136      std::map<std::string, int> retval = {
137          {"<", 1}, {"<=", 2}, {">", 3}, {">=", 4}, {"=", 5}, {"<>", 6}};
138      return retval;
139  }
140  
BM_make_map_inializer_list(benchmark::State & state)141  void BM_make_map_inializer_list(benchmark::State & state)
142  {
143      int i = 0;
144      while (state.KeepRunning()) {
145          {
146              std::map<std::string, int> map = make_map_inializer_list();
147              state.PauseTiming();
148              for (auto && x : map) {
149                  i += x.second;
150              }
151          }
152          state.ResumeTiming();
153      }
154      std::cout << "Sum of ints in all maps made=" << i << "\n";
155  }
156  
157  BENCHMARK(BM_make_map_with_boost_yap);
158  BENCHMARK(BM_make_map_with_boost_assign);
159  BENCHMARK(BM_make_map_manually);
160  BENCHMARK(BM_make_map_inializer_list);
161  
162  BENCHMARK_MAIN();
163