File: lowlevel_stream_input_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 (117 lines) | stat: -rw-r--r-- 3,088 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
// 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 <fstream>
#include <iostream>
#include <iterator>

#include <seqan3/alphabet/aminoacid/all.hpp>
#include <seqan3/io/stream/detail/fast_istreambuf_iterator.hpp>
#include <seqan3/test/performance/sequence_generator.hpp>
#include <seqan3/test/seqan2.hpp>
#include <seqan3/test/tmp_directory.hpp>

#if SEQAN3_HAS_SEQAN2
#    include <seqan/stream.h>
#endif

enum class tag
{
    std_stream_it,
    std_streambuf_it,
    seqan3_streambuf_it,
    seqan2_stream_it
};

template <tag id>
void read_all(benchmark::State & state)
{
    /* prepare file for reading */
    seqan3::test::tmp_directory tmp{};
    auto filename = tmp.path() / "foo";

    {
        std::ofstream os{filename, std::ios::binary};

        std::vector<seqan3::aa27> cont_rando = seqan3::test::generate_sequence<seqan3::aa27>(10000, 0, 0);

        for (size_t i = 0; i < 100; ++i)
            for (auto c : cont_rando)
                os.put(seqan3::to_char(c));
    }

    /* start benchmark */
    if constexpr (id == tag::std_stream_it)
    {
        for (auto _ : state)
        {
            char c{};
            std::ifstream s{filename, std::ios::binary};
            std::istream_iterator<char> it{s};
            std::istream_iterator<char> e{};

            for (; it != e; ++it)
                c += *it;

            benchmark::DoNotOptimize(c);
        }
    }
    else if constexpr (id == tag::std_streambuf_it)
    {
        for (auto _ : state)
        {
            char c{};
            std::ifstream s{filename, std::ios::binary};
            std::istreambuf_iterator<char> it{s};
            std::istreambuf_iterator<char> e{};

            for (; it != e; ++it)
                c += *it;

            benchmark::DoNotOptimize(c);
        }
    }
    else if constexpr (id == tag::seqan3_streambuf_it)
    {
        for (auto _ : state)
        {
            char c{};
            std::ifstream s{filename, std::ios::binary};
            seqan3::detail::fast_istreambuf_iterator<char> it{*s.rdbuf()};
            std::default_sentinel_t e{};

            for (; it != e; ++it)
                c += *it;

            benchmark::DoNotOptimize(c);
        }
    }
#ifdef SEQAN3_HAS_SEQAN2
    else if constexpr (id == tag::seqan2_stream_it)
    {
        for (auto _ : state)
        {
            char c{};
            std::ifstream s{filename, std::ios::binary};
            auto it = seqan2::Iter<std::ifstream, seqan2::StreamIterator<seqan2::Input>>{s};

            for (; !seqan2::atEnd(it); ++it)
                c += *it;

            benchmark::DoNotOptimize(c);
        }
    }
#endif
}

BENCHMARK_TEMPLATE(read_all, tag::std_stream_it);
BENCHMARK_TEMPLATE(read_all, tag::std_streambuf_it);
BENCHMARK_TEMPLATE(read_all, tag::seqan3_streambuf_it);
#ifdef SEQAN3_HAS_SEQAN2
BENCHMARK_TEMPLATE(read_all, tag::seqan2_stream_it);
#endif

BENCHMARK_MAIN();