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