1 // Copyright (C) 2016-2018 T. Zachary Laine
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>
8 #include <boost/assign/list_of.hpp>
13 #include <benchmark/benchmark.h>
16 template<typename Key
, typename Value
, typename Allocator
>
17 struct map_list_of_transform
19 template<typename Fn
, typename Key2
, typename Value2
>
21 boost::yap::expr_tag
<boost::yap::expr_kind::call
>,
26 boost::yap::transform(
27 boost::yap::as_expr
<boost::yap::minimal_expr
>(fn
), *this);
29 Key
{std::forward
<Key2
&&>(key
)},
30 Value
{std::forward
<Value2
&&>(value
)});
34 std::map
<Key
, Value
, Allocator
> map
;
38 template<boost::yap::expr_kind Kind
, typename Tuple
>
39 struct map_list_of_expr
41 static boost::yap::expr_kind
const kind
= Kind
;
45 template<typename Key
, typename Value
, typename Allocator
>
46 operator std::map
<Key
, Value
, Allocator
>() const
48 map_list_of_transform
<Key
, Value
, Allocator
> transform
;
49 boost::yap::transform(*this, transform
);
53 BOOST_YAP_USER_CALL_OPERATOR(::map_list_of_expr
)
56 struct map_list_of_tag
60 boost::yap::make_terminal
<map_list_of_expr
>(map_list_of_tag
{});
63 std::map
<std::string
, int> make_map_with_boost_yap()
65 return map_list_of("<", 1)("<=", 2)(">", 3)(">=", 4)("=", 5)("<>", 6);
68 void BM_make_map_with_boost_yap(benchmark::State
& state
)
71 while (state
.KeepRunning()) {
73 std::map
<std::string
, int> map
= make_map_with_boost_yap();
75 for (auto && x
: map
) {
81 std::cout
<< "Sum of ints in all maps made=" << i
<< "\n";
84 std::map
<std::string
, int> make_map_with_boost_assign()
86 return boost::assign::map_list_of("<", 1)("<=", 2)(">", 3)(">=", 4)("=", 5)(
90 void BM_make_map_with_boost_assign(benchmark::State
& state
)
93 while (state
.KeepRunning()) {
95 std::map
<std::string
, int> map
= make_map_with_boost_assign();
97 for (auto && x
: map
) {
101 state
.ResumeTiming();
103 std::cout
<< "Sum of ints in all maps made=" << i
<< "\n";
106 std::map
<std::string
, int> make_map_manually()
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);
118 void BM_make_map_manually(benchmark::State
& state
)
121 while (state
.KeepRunning()) {
123 std::map
<std::string
, int> map
= make_map_manually();
125 for (auto && x
: map
) {
129 state
.ResumeTiming();
131 std::cout
<< "Sum of ints in all maps made=" << i
<< "\n";
134 std::map
<std::string
, int> make_map_inializer_list()
136 std::map
<std::string
, int> retval
= {
137 {"<", 1}, {"<=", 2}, {">", 3}, {">=", 4}, {"=", 5}, {"<>", 6}};
141 void BM_make_map_inializer_list(benchmark::State
& state
)
144 while (state
.KeepRunning()) {
146 std::map
<std::string
, int> map
= make_map_inializer_list();
148 for (auto && x
: map
) {
152 state
.ResumeTiming();
154 std::cout
<< "Sum of ints in all maps made=" << i
<< "\n";
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
);