Skip to content

Commit

Permalink
refs #148 Возможность задавать конкретные алгоритмы при замерах
Browse files Browse the repository at this point in the history
  • Loading branch information
izvolov committed Aug 13, 2021
1 parent 228bd8f commit 49b2314
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 22 deletions.
21 changes: 15 additions & 6 deletions benchmark/burst/algorithm/integer_sort_comparison.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import numpy as np
import matplotlib.pyplot as plt
import sys

def measure (lengths, max_attempts, integer_type, prepare):
def measure (algorithms, lengths, max_attempts, integer_type, prepare):
measurements = {}

command_template =\
("@CMAKE_BINARY_DIR@/benchmark/tools/rangegen "
"--count=1 "
"--length={} | "
"@CMAKE_BINARY_DIR@/benchmark/burst/algorithm/radix "
"--algo={} "
"--attempts={} "
"--integer={} "
"--prepare={}")

for length, attempts in [(l, int(max_attempts / l)) for l in lengths]:
command = command_template.format(length, attempts, integer_type, prepare)
command = command_template.format(length, algorithms, attempts, integer_type, prepare)
sys.stderr.write(command + '\n');
results = Popen(command, shell=True, stdin=PIPE, stdout=PIPE).stdout.read().splitlines()

Expand Down Expand Up @@ -63,6 +64,11 @@ def table (lengths, measurements, integer_type, file_name):

f.write(table_string)

presets = {
'all': ['radix', 'std', 'stable', 'boost'],
'integral': ['radix', 'boost'],
}

def parse_command_line (options):
parser = argparse.ArgumentParser(options[0])
parser.add_argument('--begin', type=int, default=10,
Expand All @@ -81,14 +87,17 @@ def parse_command_line (options):
choices=['shuffle', 'ascending', 'descending', 'outlier', 'pipe-organ', 'single'])
parser.add_argument('--attempts', type=int, default=10000000,
help='Максимальное количество итераций')
parser.add_argument('--preset', type=str, default='all',
help='Набор алгоритмов, которые нужно замерить', choices=presets.keys())
args = parser.parse_args(options[1:])
return args.begin, args.end, args.step, args.prefix, args.integer, args.prepare, args.attempts
return args.begin, args.end, args.step, args.prefix, args.integer, args.prepare, args.attempts, args.preset

begin, end, step, prefix, integer_type, prepare, attempts = parse_command_line(sys.argv)
begin, end, step, prefix, integer_type, prepare, attempts, preset = parse_command_line(sys.argv)
lengths = range(begin, end, step)
algorithms = ' '.join(presets[preset])

measurements = measure(lengths, attempts, integer_type, prepare)
measurements = measure(algorithms, lengths, attempts, integer_type, prepare)

file_name = '_'.join((prefix, str(begin), str(end), str(step), integer_type, prepare, str(attempts)))
file_name = '_'.join((prefix, str(begin), str(end), str(step), integer_type, prepare, preset, str(attempts)))
plot(lengths, measurements, integer_type, file_name + '.png')
table(lengths, measurements, integer_type, file_name + '.txt')
84 changes: 68 additions & 16 deletions benchmark/burst/algorithm/radix_sort.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <utility/io/read.hpp>

#include <burst/algorithm/radix_sort.hpp>
#include <burst/functional/identity.hpp>

#include <boost/program_options.hpp>
#include <boost/sort/spreadsort/integer_sort.hpp>
Expand All @@ -10,9 +9,29 @@
#include <chrono>
#include <iostream>
#include <random>
#include <string>
#include <unordered_map>
#include <vector>

const auto burst_radix_sort_call_name = std::string("burst::radix_sort");
const auto std_sort_call_name = std::string("std::sort");
const auto std_stable_sort_call_name = std::string("std::stable_sort");
const auto boost_integer_sort_call_name = std::string("boost::integer_sort");

const auto burst_radix_sort_title = std::string("radix");
const auto std_sort_title = std::string("std");
const auto std_stable_sort_title = std::string("stable");
const auto boost_integer_sort_title = std::string("boost");

const auto default_algorithms_set =
std::vector<std::string>
{
burst_radix_sort_title,
std_sort_title,
std_stable_sort_title,
boost_integer_sort_title
};

using clock_type = std::chrono::steady_clock;

template <typename Sort, typename Container, typename UnaryFunction1, typename UnaryFunction2>
Expand Down Expand Up @@ -46,7 +65,14 @@ void
}

template <typename Integer, typename UnaryFunction1, typename UnaryFunction2>
void test_all (std::size_t attempts, UnaryFunction1 statistic, UnaryFunction2 prepare)
void
test_all
(
const std::vector<std::string> & to_bench,
std::size_t attempts,
UnaryFunction1 statistic,
UnaryFunction2 prepare
)
{
std::vector<Integer> numbers;
utility::read(std::cin, numbers);
Expand All @@ -58,31 +84,51 @@ void test_all (std::size_t attempts, UnaryFunction1 statistic, UnaryFunction2 pr
{
return burst::radix_sort(std::forward<decltype(args)>(args)..., buffer.begin());
};
test_sort("burst::radix_sort", radix_sort, numbers, attempts, statistic, prepare);

auto std_sort =
[] (auto && ... args)
{
return std::sort(std::forward<decltype(args)>(args)...);
};
test_sort("std::sort", std_sort, numbers, attempts, statistic, prepare);

auto std_stable_sort =
[] (auto && ... args)
{
return std::stable_sort(std::forward<decltype(args)>(args)...);
};
test_sort("std::stable_sort", std_stable_sort, numbers, attempts, statistic, prepare);

auto boost_int_sort =
[] (auto && ... args)
{
return boost::sort::spreadsort::integer_sort(std::forward<decltype(args)>(args)...);
};
test_sort("boost::integer_sort", boost_int_sort, numbers, attempts, statistic, prepare);

using iterator_type = typename std::vector<Integer>::iterator;
using sort_call_type = std::function<void(iterator_type, iterator_type)>;
auto sort_calls =
std::unordered_map<std::string, std::pair<std::string, sort_call_type>>
{
{burst_radix_sort_title, {burst_radix_sort_call_name, radix_sort}},
{std_sort_title, {std_sort_call_name, std_sort}},
{std_stable_sort_title, {std_stable_sort_call_name, std_stable_sort}},
{boost_integer_sort_title, {boost_integer_sort_call_name, boost_int_sort}}
};

for (const auto & sort_call_title: to_bench)
{
auto call_params = sort_calls.find(sort_call_title);
if (call_params != sort_calls.end())
{
const auto & name = call_params->second.first;
const auto & call = call_params->second.second;
test_sort(name, call, numbers, attempts, statistic, prepare);
}
else
{
const auto error_message = u8"Неверный тип тестируемого алгоритма: " + sort_call_title;
throw boost::program_options::error(error_message);
}
}
}

using test_call_type = std::function<void (std::size_t)>;
using test_call_type = std::function<void (const std::vector<std::string> &, std::size_t)>;

struct shuffle_fn
{
Expand Down Expand Up @@ -220,10 +266,11 @@ template <typename Integer, typename UnaryFunction1, typename UnaryFunction2>
test_call_type make_test_all (UnaryFunction1 statistic, UnaryFunction2 prepare)
{
return
[statistic, prepare] (std::size_t attempts)
{
return test_all<Integer>(attempts, statistic, prepare);
};
[statistic, prepare]
(const std::vector<std::string> & algorithm_set, std::size_t attempts)
{
return test_all<Integer>(algorithm_set, attempts, statistic, prepare);
};
}

template <typename T>
Expand Down Expand Up @@ -352,7 +399,11 @@ int main (int argc, const char * argv[])
"Допустимые значения: shuffle, ascending, descending, outlier, pipe-organ")
("stat", bpo::value<std::string>()->default_value("mean"),
"Статистика, которая будет рассчитана.\n"
"Допустимые значения: min, max, mean, median, sum");
"Допустимые значения: min, max, mean, median, sum")
("algo", bpo::value<std::vector<std::string>>()->multitoken()
->default_value(default_algorithms_set, "radix std stable boost"),
"Набор тестируемых алгоритмов.\n"
"Допустимые значения: radix, std, stable, boost");

try
{
Expand All @@ -370,9 +421,10 @@ int main (int argc, const char * argv[])
auto integer_type = vm["integer"].as<std::string>();
auto statistic = vm["stat"].as<std::string>();
auto prepare_type = vm["prepare"].as<std::string>();
auto algorithm_set = vm["algo"].as<std::vector<std::string>>();

auto test = dispatch_call(integer_type, statistic, prepare_type);
test(attempts);
test(algorithm_set, attempts);
}
}
catch (bpo::error & e)
Expand Down

0 comments on commit 49b2314

Please sign in to comment.