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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
// Range v3 library
//
// Copyright Eric Niebler 2013-present
// Copyright Gonzalo Brito Gadeschi 2015
//
// Use, modification and distribution is subject to 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)
//
// Project home: https://github.com/ericniebler/range-v3
//
#include <range/v3/detail/config.hpp>
#if RANGES_CXX_RETURN_TYPE_DEDUCTION >= RANGES_CXX_RETURN_TYPE_DEDUCTION_14 && \
RANGES_CXX_GENERIC_LAMBDAS >= RANGES_CXX_GENERIC_LAMBDAS_14
#include <iostream>
#include <iomanip>
#include <vector>
#include <random>
#include <functional>
#include <climits>
#include <chrono>
#include <algorithm>
#include <range/v3/all.hpp>
RANGES_DIAGNOSTIC_IGNORE_GLOBAL_CONSTRUCTORS
RANGES_DIAGNOSTIC_IGNORE_SIGN_CONVERSION
namespace
{
/// Creates an geometric infinite sequence starting at 1 where the
/// successor is multiplied by \p V
auto geometric_sequence(std::size_t V) {
std::size_t N = 1;
return ranges::views::generate([N, V]() mutable {
auto old = N;
N *= V;
return old;
});
}
/// Creates an geometric infinite sequence starting at 1 where the
/// successor is multiplied by \p V
auto geometric_sequence_n(std::size_t V, std::size_t limit) {
return geometric_sequence(V) |
ranges::views::take_while([limit](std::size_t n) { return n <= limit; });
}
/// Random uniform integer sequence
struct random_uniform_integer_sequence {
std::default_random_engine gen;
std::uniform_int_distribution<> dist;
auto operator()(std::size_t) {
return ranges::views::generate([&]{ return dist(gen); });
}
static std::string name() { return "random_uniform_integer_sequence"; }
};
struct ascending_integer_sequence {
auto operator()(std::size_t) { return ranges::views::iota(1); }
static std::string name() { return "ascending_integer_sequence"; }
};
struct descending_integer_sequence {
auto operator()(std::size_t) {
return ranges::views::iota(0ll, std::numeric_limits<long long>::max()) |
ranges::views::reverse;
}
static std::string name() { return "descending_integer_sequence"; }
};
auto even = [](auto i) { return i % 2 == 0; };
auto odd = [](auto i) { return !even(i); };
struct even_odd_integer_sequence {
static std::string name() { return "even_odd_integer_sequence"; }
auto operator()(std::size_t n) {
return ranges::views::concat(ranges::views::ints(std::size_t{0}, n) | ranges::views::filter(even),
ranges::views::ints(std::size_t{0}, n) | ranges::views::filter(odd));
}
};
struct organ_pipe_integer_sequence {
static std::string name() { return "organ_pipe_integer_sequence"; }
auto operator()(std::size_t n) {
return ranges::views::concat(ranges::views::ints(std::size_t{0}, n/2),
ranges::views::ints(std::size_t{0}, n/2 + 1)
| ranges::views::reverse);
}
};
template<typename Seq>
void print(Seq seq, std::size_t n) {
std::cout << "sequence: " << seq.name() << '\n';
RANGES_FOR(auto i, seq(n) | ranges::views::take(n)) {
std::cout << i << '\n';
}
}
/// Returns the duration of a computation
using clock_t = std::chrono::high_resolution_clock;
using duration_t = clock_t::duration;
template<typename Computation>
auto duration(Computation &&c) {
auto time = []{ return clock_t::now(); };
const auto start = time();
c();
return time() - start;
}
template<typename Duration>
auto to_millis(Duration d) {
return std::chrono::duration_cast<std::chrono::milliseconds>(d).count();
}
template<typename Durations> auto compute_mean(Durations &&durations) {
using D = ranges::range_value_t<Durations>;
D total = ranges::accumulate(durations, D{}, ranges::plus{}, ranges::convert_to<D>{});
return total / ranges::size(durations);
}
template<typename Durations> auto compute_stddev(Durations &&durations) {
using D = ranges::range_value_t<Durations>;
using Rep = typename D::rep;
const auto mean = compute_mean(durations);
const auto stddev = ranges::accumulate(
durations | ranges::views::transform([=](auto i) {
auto const delta = (i - mean).count();
return delta * delta;
}), Rep{}, ranges::plus{}, ranges::convert_to<Rep>{});
return D{static_cast<typename D::rep>(std::sqrt(stddev / ranges::size(durations)))};
}
struct benchmark {
struct result_t {
duration_t mean_t;
duration_t max_t;
duration_t min_t;
std::size_t size;
duration_t deviation;
};
std::vector<result_t> results;
template<typename Computation, typename Sizes>
benchmark(Computation &&c, Sizes &&sizes, double target_deviation = 0.25,
std::size_t max_iters = 100, std::size_t min_iters = 5) {
RANGES_FOR(auto size, sizes) {
std::vector<duration_t> durations;
duration_t deviation;
duration_t mean_duration;
std::size_t iter;
for (iter = 0; iter < max_iters; ++iter) {
c.init(size);
durations.emplace_back(duration(c));
mean_duration = compute_mean(durations);
if (++iter == max_iters) {
break;
}
if (iter >= min_iters) {
deviation = compute_stddev(durations);
if (deviation < target_deviation * mean_duration)
break;
}
}
auto minmax = ranges::minmax(durations);
results.emplace_back(
result_t{mean_duration, minmax.max, minmax.min, size, deviation});
std::cerr << "size: " << size << " iter: " << iter
<< " dev: " << to_millis(deviation)
<< " mean: " << to_millis(mean_duration)
<< " max: " << to_millis(minmax.max)
<< " min: " << to_millis(minmax.min) << '\n';
}
}
};
template<typename Seq, typename Comp>
struct computation_on_sequence {
Seq seq;
Comp comp;
std::vector<ranges::range_value_t<decltype(seq(std::size_t{}))>> data;
computation_on_sequence(Seq s, Comp c, std::size_t max_size)
: seq(std::move(s)), comp(std::move(c)) {
data.reserve(max_size);
}
void init(std::size_t size) {
data.resize(size);
ranges::copy(seq(size) | ranges::views::take(size), ranges::begin(data));
}
void operator()() { comp(data); }
};
template<typename Seq, typename Comp>
auto make_computation_on_sequence(Seq s, Comp c, std::size_t max_size) {
return computation_on_sequence<Seq, Comp>(std::move(s), std::move(c),
max_size);
}
template<typename Seq> void benchmark_sort(Seq &&seq, std::size_t max_size) {
auto ranges_sort_comp =
make_computation_on_sequence(seq, ranges::sort, max_size);
auto std_sort_comp = make_computation_on_sequence(
seq, [](auto &&v) { std::sort(std::begin(v), std::end(v)); }, max_size);
auto ranges_sort_benchmark =
benchmark(ranges_sort_comp, geometric_sequence_n(2, max_size));
auto std_sort_benchmark =
benchmark(std_sort_comp, geometric_sequence_n(2, max_size));
using std::setw;
std::cout << '#'
<< "pattern: " << seq.name() << '\n';
std::cout << '#' << setw(19) << 'N' << setw(20) << "ranges::sort" << setw(20)
<< "std::sort"
<< '\n';
RANGES_FOR(auto p, ranges::views::zip(ranges_sort_benchmark.results,
std_sort_benchmark.results)) {
auto rs = p.first;
auto ss = p.second;
std::cout << setw(20) << rs.size << setw(20) << to_millis(rs.mean_t)
<< setw(20) << to_millis(ss.mean_t) << '\n';
}
}
} // unnamed namespace
int main()
{
constexpr std::size_t max_size = 2000000;
print(random_uniform_integer_sequence(), 20);
print(ascending_integer_sequence(), 20);
print(descending_integer_sequence(), 20);
print(even_odd_integer_sequence(), 20);
print(organ_pipe_integer_sequence(), 20);
benchmark_sort(random_uniform_integer_sequence(), max_size);
benchmark_sort(ascending_integer_sequence(), max_size);
benchmark_sort(descending_integer_sequence(), max_size);
benchmark_sort(organ_pipe_integer_sequence(), max_size);
}
#else
#pragma message("sort_patterns requires C++14 return type deduction and generic lambdas")
int main() {}
#endif
|