Add map_assign_perf.cpp.

This commit is contained in:
Zach Laine 2016-12-15 18:43:16 -06:00
parent c96db7812f
commit 498a43da34
2 changed files with 189 additions and 0 deletions

View File

@ -2,6 +2,8 @@ find_program(lldb lldb)
find_program(gdb gdb)
include_directories(${CMAKE_HOME_DIRECTORY})
include_directories(${CMAKE_HOME_DIRECTORY}/benchmark-v1.1.0/include)
macro(add_code_gen_executable name)
add_executable(${name} ${name}.cpp)
@ -14,6 +16,17 @@ add_code_gen_executable(code_gen_samples)
add_code_gen_executable(map_assign_code_gen)
macro(add_perf_executable name)
add_executable(${name} ${name}.cpp)
target_link_libraries(${name} benchmark)
if (clang_on_linux)
target_link_libraries(${name} c++)
endif ()
endmacro()
add_perf_executable(map_assign_perf)
if (lldb)
add_custom_target(
perf
@ -26,6 +39,8 @@ if (lldb)
COMMAND lldb -f $<TARGET_FILE:map_assign_code_gen> -o "disassemble --name make_map_with_boost_assign" -o quit
COMMAND lldb -f $<TARGET_FILE:map_assign_code_gen> -o "disassemble --name make_map_manually" -o quit
COMMAND lldb -f $<TARGET_FILE:map_assign_code_gen> -o "disassemble --name make_map_inializer_list" -o quit
COMMAND map_assign_perf
)
elseif (gdb)
add_custom_target(
@ -39,5 +54,7 @@ elseif (gdb)
COMMAND gdb -se $<TARGET_FILE:map_assign_code_gen> -batch -ex "disassemble make_map_with_boost_assign"
COMMAND gdb -se $<TARGET_FILE:map_assign_code_gen> -batch -ex "disassemble make_map_manually"
COMMAND gdb -se $<TARGET_FILE:map_assign_code_gen> -batch -ex "disassemble make_map_inializer_list"
COMMAND map_assign_perf
)
endif()

172
perf/map_assign_perf.cpp Normal file
View File

@ -0,0 +1,172 @@
#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::call_tag, Fn const & fn, Key2 && key, Value2 && value)
{
boost::yap::transform(fn, *this);
map.try_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
{
using this_type = map_list_of_expr<Kind, Tuple>;
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_MEMBER_CALL_OPERATOR(this_type, ::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()