Skip to content

Instantly share code, notes, and snippets.

@ldionne
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ldionne/3c530ee38a965ff378f3 to your computer and use it in GitHub Desktop.
Save ldionne/3c530ee38a965ff378f3 to your computer and use it in GitHub Desktop.
Various implementations of degenerate use case presented at http://thread.gmane.org/gmane.comp.lib.boost.devel/260913/focus=261035
<%
hana_tuple = (1..200).step(10)
hana_map = (1..50).step(10).to_a
mpl = hana_tuple
%>
{
"title": {
"text": "Executable size"
},
"yAxis": {
"title": {
"text": "Executable size (kb)"
},
"floor": 0
},
"tooltip": {
"valueSuffix": "kb"
},
"series": [
{
"name": "hana::tuple",
"data": <%= measure(:bloat, 'hana.tuple.erb.cpp', hana_tuple) %>
}, {
"name": "hana::map (via insert)",
"data": <%= measure(:bloat, 'hana.map.insert.erb.cpp', hana_map) %>
}, {
"name": "hana::map (via insert + decltype)",
"data": <%= measure(:bloat, 'hana.map.insert.decltype.erb.cpp', hana_map) %>
}, {
"name": "hana::map (via unpack)",
"data": <%= measure(:bloat, 'hana.map.unpack.erb.cpp', hana_map) %>
}
<% if cmake_bool("@Boost_FOUND@") %>
, {
"name": "mpl::vector",
"data": <%= measure(:bloat, 'mpl.vector.erb.cpp', mpl) %>
}, {
"name": "mpl::map",
"data": <%= measure(:bloat, 'mpl.map.erb.cpp', mpl) %>
}
<% end %>
]
}
<%
hana_tuple = (1...50).step(5).to_a + (50..200).step(10).to_a
hana_map = (1..50).step(10).to_a
mpl = hana_tuple
%>
{
"title": {
"text": "Compile-time behavior"
},
"series": [
{
"name": "hana::tuple",
"data": <%= time_compilation('hana.tuple.erb.cpp', hana_tuple) %>
}, {
"name": "hana::map (via insert)",
"data": <%= time_compilation('hana.map.insert.erb.cpp', hana_map) %>
}, {
"name": "hana::map (via insert + decltype)",
"data": <%= time_compilation('hana.map.insert.decltype.erb.cpp', hana_map) %>
}, {
"name": "hana::map (via unpack)",
"data": <%= time_compilation('hana.map.unpack.erb.cpp', hana_map) %>
}
<% if cmake_bool("@Boost_FOUND@") %>
, {
"name": "mpl::vector",
"data": <%= time_compilation('mpl.vector.erb.cpp', mpl) %>
}, {
"name": "mpl::map",
"data": <%= time_compilation('mpl.map.erb.cpp', mpl) %>
}
<% end %>
]
}
<%
hana_tuple = (1...50).step(5).to_a + (50..200).step(10).to_a
hana_map = (1..50).step(10).to_a
mpl = hana_tuple
%>
{
"title": {
"text": "Runtime behavior"
},
"series": [
{
"name": "hana::tuple",
"data": <%= time_execution('hana.tuple.erb.cpp', hana_tuple) %>
}, {
"name": "hana::map (via insert)",
"data": <%= time_execution('hana.map.insert.erb.cpp', hana_map) %>
}, {
"name": "hana::map (via insert + decltype)",
"data": <%= time_execution('hana.map.insert.decltype.erb.cpp', hana_map) %>
}, {
"name": "hana::map (via unpack)",
"data": <%= time_execution('hana.map.unpack.erb.cpp', hana_map) %>
}
<% if cmake_bool("@Boost_FOUND@") %>
, {
"name": "mpl::vector",
"data": <%= time_execution('mpl.vector.erb.cpp', mpl) %>
}, {
"name": "mpl::map",
"data": <%= time_execution('mpl.map.erb.cpp', mpl) %>
}
<% end %>
]
}
/*
@copyright Louis Dionne 2015
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/map.hpp>
#include <boost/hana/pair.hpp>
#include <boost/hana/range.hpp>
#include <boost/hana/type.hpp>
#include <array>
#include "measure.hpp"
using namespace boost::hana;
static constexpr int INPUT_SIZE = <%= input_size %>;
static constexpr int INDEX = (INPUT_SIZE * 2) / 3;
static constexpr int LOOPS = 10000;
void run() {
auto get_array = [](auto index) {
auto inserter = [](auto map, auto n) {
using Array = std::array<char, decltype(n)::value>;
return insert(map, make_pair(n, type<Array>));
};
auto arrays = fold.left(range(int_<0>, int_<INPUT_SIZE>), make_map(), inserter);
return arrays[index];
};
constexpr decltype(get_array(int_<INDEX>))::type array{};
static_assert(array.size() == INDEX, "");
}
int main() {
boost::hana::benchmark::measure([] {
volatile int c = LOOPS;
for (int i = 0; i < c; ++i)
run();
});
}
/*
@copyright Louis Dionne 2015
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/map.hpp>
#include <boost/hana/pair.hpp>
#include <boost/hana/range.hpp>
#include <boost/hana/type.hpp>
#include <array>
#include "measure.hpp"
using namespace boost::hana;
static constexpr int INPUT_SIZE = <%= input_size %>;
static constexpr int INDEX = (INPUT_SIZE * 2) / 3;
static constexpr int LOOPS = 10000;
void run() {
auto inserter = [](auto map, auto n) {
using Array = std::array<char, decltype(n)::value>;
return insert(map, make_pair(n, type<Array>));
};
auto arrays = fold.left(range(int_<0>, int_<INPUT_SIZE>), make_map(), inserter);
auto Array = arrays[int_<INDEX>];
constexpr decltype(Array)::type array{};
static_assert(array.size() == INDEX, "");
}
int main() {
boost::hana::benchmark::measure([] {
volatile int c = LOOPS;
for (int i = 0; i < c; ++i)
run();
});
}
/*
@copyright Louis Dionne 2015
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/map.hpp>
#include <boost/hana/pair.hpp>
#include <boost/hana/range.hpp>
#include <boost/hana/type.hpp>
#include <array>
#include "measure.hpp"
using namespace boost::hana;
static constexpr int INPUT_SIZE = <%= input_size %>;
static constexpr int INDEX = (INPUT_SIZE * 2) / 3;
static constexpr int LOOPS = 10000;
void run() {
auto arrays = unpack(range(int_<0>, int_<INPUT_SIZE>), [](auto ...n) {
return make_map(
make_pair(n, type<std::array<char, decltype(n)::value>>)...
);
});
auto Array = arrays[int_<INDEX>];
constexpr decltype(Array)::type array{};
static_assert(array.size() == INDEX, "");
}
int main() {
boost::hana::benchmark::measure([] {
volatile int c = LOOPS;
for (int i = 0; i < c; ++i)
run();
});
}
/*
@copyright Louis Dionne 2015
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/range.hpp>
#include <boost/hana/tuple.hpp>
#include <array>
#include "measure.hpp"
using namespace boost::hana;
static constexpr int INPUT_SIZE = <%= input_size %>;
static constexpr int INDEX = (INPUT_SIZE * 2) / 3;
static constexpr int LOOPS = 10000;
void run() {
auto arrays = unpack(range(int_<0>, int_<INPUT_SIZE>), [](auto ...n) {
return tuple_t<std::array<char, decltype(n)::value>...>;
});
auto Array = arrays[int_<INDEX>];
constexpr decltype(Array)::type array{};
static_assert(array.size() == INDEX, "");
}
int main() {
boost::hana::benchmark::measure([] {
volatile int c = LOOPS;
for (int i = 0; i < c; ++i)
run();
});
}
/*
@copyright Louis Dionne 2015
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
#include <boost/mpl/at.hpp>
#include <boost/mpl/insert.hpp>
#include <boost/mpl/inserter.hpp>
#include <boost/mpl/map.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/transform.hpp>
#include <array>
#include "measure.hpp"
namespace mpl = boost::mpl;
static constexpr int INPUT_SIZE = <%= input_size %>;
static constexpr int INDEX = (INPUT_SIZE * 2) / 3;
static constexpr int LOOPS = 10000;
template <typename T, typename Size>
struct make_array {
using type = std::array<T, Size::value>;
};
void run() {
using Range = mpl::range_c<int, 0, INPUT_SIZE>;
using Arrays = mpl::transform<
Range,
mpl::pair<mpl::_, make_array<char, mpl::_>>,
mpl::inserter<mpl::map<>, mpl::insert<mpl::_1, mpl::_2>>
>::type;
using Array = mpl::at<Arrays, mpl::integral_c<int, INDEX>>::type;
constexpr Array array{};
static_assert(array.size() == INDEX, "");
}
int main() {
boost::hana::benchmark::measure([] {
volatile int c = LOOPS;
for (int i = 0; i < c; ++i)
run();
});
}
/*
@copyright Louis Dionne 2015
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
*/
#include <boost/mpl/at.hpp>
#include <boost/mpl/back_inserter.hpp>
#include <boost/mpl/push_back.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/vector.hpp>
#include <array>
#include "measure.hpp"
namespace mpl = boost::mpl;
static constexpr int INPUT_SIZE = <%= input_size %>;
static constexpr int INDEX = (INPUT_SIZE * 2) / 3;
static constexpr int LOOPS = 10000;
template <typename T, typename Size>
struct make_array {
using type = std::array<T, Size::value>;
};
void run() {
using Range = mpl::range_c<int, 0, INPUT_SIZE>;
using Arrays = mpl::transform<
Range,
make_array<char, mpl::_>,
mpl::back_inserter<mpl::vector<>>
>::type;
using Array = mpl::at<Arrays, mpl::integral_c<int, INDEX>>::type;
constexpr Array array{};
static_assert(array.size() == INDEX, "");
}
int main() {
boost::hana::benchmark::measure([] {
volatile int c = LOOPS;
for (int i = 0; i < c; ++i)
run();
});
}
@ldionne
Copy link
Author

ldionne commented Jun 9, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment