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
|
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
#include <benchmark/benchmark.h>
#include <deque>
#include <forward_list>
#include <list>
#include <random>
#include <string>
#include <vector>
#include <seqan3/utility/views/single_pass_input.hpp>
// ============================================================================
// sequential_read
// ============================================================================
template <typename container_t, typename adaptor_t, bool single_pass = false>
void sequential_read(benchmark::State & state)
{
container_t c;
c.resize(1'000'000);
uint8_t i = 0;
for (auto & e : c)
e = ++i; // dummy values
uint8_t dummy = 0;
// if single_pass, add seqan3::views::single_pass_input, otherwise just &
using single_t = std::conditional_t<single_pass, decltype(c | seqan3::views::single_pass_input), container_t &>;
if constexpr (std::same_as<adaptor_t, void>)
{
for (auto _ : state)
{
single_t s{c};
for (auto e : s)
dummy += e;
}
}
else
{
auto adaptor = adaptor_t{}(1000);
for (auto _ : state)
{
single_t s{c};
auto v = s | adaptor | adaptor | adaptor | adaptor | adaptor;
for (auto e : v)
dummy += e;
}
}
[[maybe_unused]] volatile uint8_t dummy2 = dummy;
}
BENCHMARK_TEMPLATE(sequential_read, std::string, void);
BENCHMARK_TEMPLATE(sequential_read, std::string, decltype(std::views::drop));
BENCHMARK_TEMPLATE(sequential_read, std::vector<uint8_t>, void);
BENCHMARK_TEMPLATE(sequential_read, std::vector<uint8_t>, decltype(std::views::drop));
BENCHMARK_TEMPLATE(sequential_read, std::deque<uint8_t>, void);
BENCHMARK_TEMPLATE(sequential_read, std::deque<uint8_t>, decltype(std::views::drop));
BENCHMARK_TEMPLATE(sequential_read, std::list<uint8_t>, void);
BENCHMARK_TEMPLATE(sequential_read, std::list<uint8_t>, decltype(std::views::drop));
BENCHMARK_TEMPLATE(sequential_read, std::forward_list<uint8_t>, void);
BENCHMARK_TEMPLATE(sequential_read, std::forward_list<uint8_t>, decltype(std::views::drop));
BENCHMARK_TEMPLATE(sequential_read, std::vector<uint8_t>, void, true);
BENCHMARK_TEMPLATE(sequential_read, std::vector<uint8_t>, decltype(std::views::drop), true);
BENCHMARK_TEMPLATE(sequential_read, std::forward_list<uint8_t>, void, true);
BENCHMARK_TEMPLATE(sequential_read, std::forward_list<uint8_t>, decltype(std::views::drop), true);
// ============================================================================
// random access
// ============================================================================
template <typename container_t, typename adaptor_t>
void random_access(benchmark::State & state)
{
container_t c;
c.resize(1'005'000); // 5'000 more that will be dropped
uint8_t i = 0;
for (auto & e : c)
e = ++i; // dummy values
std::vector<size_t> access_positions;
access_positions.resize(1'000'000);
std::mt19937 gen(42);
std::uniform_int_distribution<size_t> dis(0, 1'000'000 - 1);
for (size_t i = 0; i < 1'000'000; ++i)
access_positions[i] = dis(gen);
size_t dummy = 0;
if constexpr (std::same_as<adaptor_t, void>)
{
for (auto _ : state)
{
for (auto i : access_positions)
dummy += c[i];
}
}
else
{
auto adaptor = adaptor_t{}(1000);
for (auto _ : state)
{
auto v = c | adaptor | adaptor | adaptor | adaptor | adaptor;
for (auto i : access_positions)
dummy += v[i];
}
}
[[maybe_unused]] volatile size_t dummy2 = dummy;
}
BENCHMARK_TEMPLATE(random_access, std::string, void);
BENCHMARK_TEMPLATE(random_access, std::string, decltype(std::views::drop));
BENCHMARK_TEMPLATE(random_access, std::vector<uint8_t>, void);
BENCHMARK_TEMPLATE(random_access, std::vector<uint8_t>, decltype(std::views::drop));
BENCHMARK_TEMPLATE(random_access, std::deque<uint8_t>, void);
BENCHMARK_TEMPLATE(random_access, std::deque<uint8_t>, decltype(std::views::drop));
// ============================================================================
// run
// ============================================================================
BENCHMARK_MAIN();
|