File: charconv_from_chars_benchmark.cpp

package info (click to toggle)
seqan3 3.0.2%2Bds-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,052 kB
  • sloc: cpp: 144,641; makefile: 1,288; ansic: 294; sh: 228; xml: 217; javascript: 50; python: 27; php: 25
file content (175 lines) | stat: -rw-r--r-- 5,003 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
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
// -----------------------------------------------------------------------------------------------------
// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
// -----------------------------------------------------------------------------------------------------

#include <benchmark/benchmark.h>

#include <cstring>
#include <sstream>
#include <string_view>

#include <seqan3/std/charconv>

// -----------------------------------------------------------------------------
// from_char for integral types
// -----------------------------------------------------------------------------
char const * str = "122";

template <typename arithmetic_type>
static void from_char(benchmark::State & state)
{
    arithmetic_type val{};
    size_t sum{};

    for (auto _ : state)
    {
        std::from_chars(&str[0], &str[0] + sizeof(str), val);
        sum += val;
    }

    // prevent complete optimisation
    [[maybe_unused]] volatile auto fin = sum;
}

BENCHMARK_TEMPLATE(from_char, int8_t);
BENCHMARK_TEMPLATE(from_char, uint8_t);
BENCHMARK_TEMPLATE(from_char, int16_t);
BENCHMARK_TEMPLATE(from_char, uint16_t);
BENCHMARK_TEMPLATE(from_char, int32_t);
BENCHMARK_TEMPLATE(from_char, uint32_t);
BENCHMARK_TEMPLATE(from_char, int64_t);
BENCHMARK_TEMPLATE(from_char, uint64_t);

template <typename arithmetic_type>
static void from_stream(benchmark::State & state)
{
    arithmetic_type val{};
    size_t sum{};

    for (auto _ : state)
    {
        std::stringstream ss;
        ss << str;
        ss >> val;
        sum += val;
    }

    // prevent complete optimisation
    [[maybe_unused]] volatile auto fin = sum;
}

BENCHMARK_TEMPLATE(from_stream, int8_t);
BENCHMARK_TEMPLATE(from_stream, uint8_t);
BENCHMARK_TEMPLATE(from_stream, int16_t);
BENCHMARK_TEMPLATE(from_stream, uint16_t);
BENCHMARK_TEMPLATE(from_stream, int32_t);
BENCHMARK_TEMPLATE(from_stream, uint32_t);
BENCHMARK_TEMPLATE(from_stream, int64_t);
BENCHMARK_TEMPLATE(from_stream, uint64_t);

template <typename arithmetic_type>
static void from_atol(benchmark::State & state)
{
    arithmetic_type val{};
    size_t sum{};

    for (auto _ : state)
    {
        val = atol(str);
        sum += val;
    }

    // prevent complete optimisation
    [[maybe_unused]] volatile auto fin = sum;
}

BENCHMARK_TEMPLATE(from_atol, int8_t);
BENCHMARK_TEMPLATE(from_atol, uint8_t);
BENCHMARK_TEMPLATE(from_atol, int16_t);
BENCHMARK_TEMPLATE(from_atol, uint16_t);
BENCHMARK_TEMPLATE(from_atol, int32_t);
BENCHMARK_TEMPLATE(from_atol, uint32_t);
BENCHMARK_TEMPLATE(from_atol, int64_t);
BENCHMARK_TEMPLATE(from_atol, uint64_t);

#if __has_include(<boost/spirit/include/qi.hpp>)

#include <boost/spirit/include/qi.hpp>

template <typename arithmetic_type>
static void from_boost(benchmark::State & state)
{
    arithmetic_type val{};
    size_t sum{};

    for (auto _ : state)
    {
        auto it = &str[0];
        boost::spirit::qi::phrase_parse(it, &str[0] + sizeof(str),
                                        boost::spirit::qi::int_, boost::spirit::ascii::space, val);
        sum += val;
    }

    // prevent complete optimisation
    [[maybe_unused]] volatile auto fin = sum;
}

BENCHMARK_TEMPLATE(from_boost, int8_t);
BENCHMARK_TEMPLATE(from_boost, uint8_t);
BENCHMARK_TEMPLATE(from_boost, int16_t);
BENCHMARK_TEMPLATE(from_boost, uint16_t);
BENCHMARK_TEMPLATE(from_boost, int32_t);
BENCHMARK_TEMPLATE(from_boost, uint32_t);
BENCHMARK_TEMPLATE(from_boost, int64_t);
BENCHMARK_TEMPLATE(from_boost, uint64_t);

#endif // __has_include(<boost/spirit/include/qi.hpp>)

// -----------------------------------------------------------------------------
// from_char for floating point types
// -----------------------------------------------------------------------------
char const * str_float = "122.45e-2";

template <typename arithmetic_type>
static void from_chars_to_float(benchmark::State & state)
{
    arithmetic_type val{};
    size_t sum{};

    for (auto _ : state)
    {
        std::from_chars(&str_float[0], &str_float[0] + sizeof(str_float), val);
        sum += val;
    }

    // prevent complete optimisation
    [[maybe_unused]] volatile auto fin = sum;
}

template <typename arithmetic_type>
static void from_stream_to_float(benchmark::State & state)
{
    arithmetic_type val{};
    size_t sum{};

    for (auto _ : state)
    {
        std::stringstream ss;
        ss << str_float;
        ss >> val;
        sum += val;
    }

    // prevent complete optimisation
    [[maybe_unused]] volatile auto fin = sum;
}

BENCHMARK_TEMPLATE(from_chars_to_float, float);
BENCHMARK_TEMPLATE(from_chars_to_float, double);
BENCHMARK_TEMPLATE(from_stream_to_float, float);
BENCHMARK_TEMPLATE(from_stream_to_float, double);

BENCHMARK_MAIN();