File: container_assignment_benchmark.cpp

package info (click to toggle)
seqan3 3.4.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,580 kB
  • sloc: cpp: 145,192; sh: 307; xml: 264; javascript: 95; makefile: 70; perl: 29; php: 15
file content (135 lines) | stat: -rw-r--r-- 5,322 bytes parent folder | download
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
// 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 <seqan3/alphabet/aminoacid/aa27.hpp>
#include <seqan3/alphabet/nucleotide/dna4.hpp>
#include <seqan3/alphabet/nucleotide/dna5.hpp>
#include <seqan3/test/literal/bytes.hpp>
#include <seqan3/test/performance/sequence_generator.hpp>

using namespace seqan3::test::literals;

#ifndef NDEBUG
static constexpr size_t vector_size{1_MiB};
#else
static constexpr size_t vector_size{16_MiB};
#endif // NDEBUG

enum tag
{
    assignment_operator,
    std_copy,
    uninitialized_copy
};

template <tag id>
struct assignment_functor
{
    template <typename container_t>
        requires (id == tag::assignment_operator)
    static constexpr void call(container_t & to, container_t const & from)
        noexcept(noexcept(std::is_nothrow_assignable_v<container_t, container_t>))
    {
        benchmark::DoNotOptimize(to = from);
    }

    template <typename container_t>
        requires (id == tag::std_copy)
    static constexpr void call(container_t & to, container_t const & from) noexcept
    {
        std::copy(std::ranges::begin(from), std::ranges::end(from), std::ranges::begin(to));
        benchmark::DoNotOptimize(to);
    }

    template <typename container_t>
        requires (id == tag::uninitialized_copy)
    static constexpr void call(container_t & to, container_t const & from) noexcept
    {
        std::uninitialized_copy(std::ranges::begin(from), std::ranges::end(from), std::ranges::begin(to));
        benchmark::DoNotOptimize(to);
    }
};

template <typename container_t>
    requires requires (container_t v) { v.resize(1u); }
static constexpr void resize(container_t & container, size_t const size)
    noexcept(noexcept(std::declval<container_t>().resize(1u)))
{
    container.resize(size);
}

#if SEQAN3_HAS_SEQAN2
template <typename container_t>
static constexpr void resize(container_t & container, size_t const size)
    noexcept(noexcept(seqan2::resize(std::declval<container_t>(), 1u)))
{
    seqan2::resize(container, size);
}
#endif // SEQAN3_HAS_SEQAN2

template <tag id, template <typename, typename...> typename container_t, typename alphabet_t, typename... args>
static void assign(benchmark::State & state)
{
    auto random_sequence = []() constexpr
    {
        if constexpr (seqan3::alphabet<alphabet_t>)
            return seqan3::test::generate_sequence<alphabet_t>(vector_size);
#if SEQAN3_HAS_SEQAN2
        else
            return seqan3::test::generate_sequence_seqan2<alphabet_t>(vector_size);
#endif // SEQAN3_HAS_SEQAN2
    }();

    container_t<alphabet_t, args...> from{};
    resize(from, vector_size);
    std::copy(std::ranges::begin(random_sequence), std::ranges::end(random_sequence), std::ranges::begin(from));
    container_t<alphabet_t, args...> to{};
    resize(to, vector_size);
    assignment_functor<id> fn{};

    for (auto _ : state)
    {
        fn.call(to, from);
        benchmark::ClobberMemory();
    }
}

BENCHMARK_TEMPLATE(assign, tag::assignment_operator, std::vector, seqan3::dna4);
BENCHMARK_TEMPLATE(assign, tag::assignment_operator, std::vector, seqan3::dna5);
BENCHMARK_TEMPLATE(assign, tag::assignment_operator, std::vector, seqan3::aa27);

BENCHMARK_TEMPLATE(assign, tag::std_copy, std::vector, seqan3::dna4);
BENCHMARK_TEMPLATE(assign, tag::std_copy, std::vector, seqan3::dna5);
BENCHMARK_TEMPLATE(assign, tag::std_copy, std::vector, seqan3::aa27);

BENCHMARK_TEMPLATE(assign, tag::uninitialized_copy, std::vector, seqan3::dna4);
BENCHMARK_TEMPLATE(assign, tag::uninitialized_copy, std::vector, seqan3::dna5);
BENCHMARK_TEMPLATE(assign, tag::uninitialized_copy, std::vector, seqan3::aa27);

#if SEQAN3_HAS_SEQAN2
BENCHMARK_TEMPLATE(assign, tag::assignment_operator, std::vector, seqan2::Dna);
BENCHMARK_TEMPLATE(assign, tag::assignment_operator, std::vector, seqan2::Dna5);
BENCHMARK_TEMPLATE(assign, tag::assignment_operator, std::vector, seqan2::AminoAcid);
BENCHMARK_TEMPLATE(assign, tag::assignment_operator, seqan2::String, seqan2::Dna);
BENCHMARK_TEMPLATE(assign, tag::assignment_operator, seqan2::String, seqan2::Dna5);
BENCHMARK_TEMPLATE(assign, tag::assignment_operator, seqan2::String, seqan2::AminoAcid);

BENCHMARK_TEMPLATE(assign, tag::std_copy, std::vector, seqan2::Dna);
BENCHMARK_TEMPLATE(assign, tag::std_copy, std::vector, seqan2::Dna5);
BENCHMARK_TEMPLATE(assign, tag::std_copy, std::vector, seqan2::AminoAcid);
BENCHMARK_TEMPLATE(assign, tag::std_copy, seqan2::String, seqan2::Dna);
BENCHMARK_TEMPLATE(assign, tag::std_copy, seqan2::String, seqan2::Dna5);
BENCHMARK_TEMPLATE(assign, tag::std_copy, seqan2::String, seqan2::AminoAcid);

BENCHMARK_TEMPLATE(assign, tag::uninitialized_copy, std::vector, seqan2::Dna);
BENCHMARK_TEMPLATE(assign, tag::uninitialized_copy, std::vector, seqan2::Dna5);
BENCHMARK_TEMPLATE(assign, tag::uninitialized_copy, std::vector, seqan2::AminoAcid);
BENCHMARK_TEMPLATE(assign, tag::uninitialized_copy, seqan2::String, seqan2::Dna);
BENCHMARK_TEMPLATE(assign, tag::uninitialized_copy, seqan2::String, seqan2::Dna5);
BENCHMARK_TEMPLATE(assign, tag::uninitialized_copy, seqan2::String, seqan2::AminoAcid);
#endif // SEQAN3_HAS_SEQAN2

BENCHMARK_MAIN();