File: performance.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (341 lines) | stat: -rw-r--r-- 12,232 bytes parent folder | download | duplicates (6)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Boost.Convert test and usage example
// Copyright (c) 2009-2020 Vladimir Batov.
// Use, modification and distribution are subject to the Boost Software License,
// Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.

#include "./test.hpp"

#if !defined(BOOST_CONVERT_CXX14) || defined(BOOST_NO_CXX17_HDR_CHARCONV)
int main(int, char const* []) { return 0; }
#else

#include "./prepare.hpp"
#include <boost/convert.hpp>
#include <boost/convert/stream.hpp>
#include <boost/convert/printf.hpp>
#include <boost/convert/strtol.hpp>
#include <boost/convert/spirit.hpp>
#include <boost/convert/charconv.hpp>
#include <boost/convert/lexical_cast.hpp>
#include <boost/timer/timer.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <array>

using std::string;
using boost::convert;

namespace cnv = boost::cnv;
namespace arg = boost::cnv::parameter;

namespace { namespace local
{
    template<typename Type>
    struct array
    {
        using type = std::array<Type, 20>;
    };
    template<typename T> static typename array<T>::type const& get();

    int BOOST_CONSTEXPR_OR_CONST num_cycles = 1000000;
    int                  sum = 0;

    struct timer : boost::timer::cpu_timer
    {
        using this_type = timer;
        using base_type = boost::timer::cpu_timer;

        double value() const
        {
            boost::timer::cpu_times times = base_type::elapsed();
            int const             use_sum = (sum % 2) ? 0 : (sum % 2); BOOST_TEST(use_sum == 0);

            return double(times.user + times.system) / 1000000000 + use_sum;
        }
    };
    template<            typename Type, typename Cnv> static double str_to (Cnv const&);
    template<typename S, typename Type, typename Cnv> static double to_str (Cnv const&);

    template<>
    local::array<int>::type const&
    get<int>()
    {
        static array<int>::type ints;
        static bool           filled;

        if (!filled)
        {
            boost::random::mt19937                     gen (::time(0));
            boost::random::uniform_int_distribution<> dist (INT_MIN, INT_MAX); // INT_MAX(32) = 2,147,483,647

            for (size_t k = 0; k < ints.size(); ++k)
                ints[k] = dist(gen);

            filled = true;
        }
        return ints;
    }
    template<>
    array<long int>::type const&
    get<long int>()
    {
        static array<long int>::type ints;
        static bool                filled;

        if (!filled)
        {
            boost::random::mt19937                     gen (::time(0));
            boost::random::uniform_int_distribution<> dist (INT_MIN, INT_MAX); // INT_MAX(32) = 2147483647

            for (size_t k = 0; k < ints.size(); ++k)
                ints[k] = dist(gen);

            filled = true;
        }
        return ints;
    }
    template<>
    array<double>::type const&
    get<double>()
    {
        static array<double>::type dbls;
        static bool              filled;

        if (!filled)
        {
            boost::random::mt19937                     gen (::time(0));
            boost::random::uniform_int_distribution<> dist (INT_MIN, INT_MAX); // INT_MAX(32) = 2147483647

            for (size_t k = 0; k < dbls.size(); ++k)
                dbls[k] = double(dist(gen)) + 0.7654321;

            filled = true;
        }
        return dbls;
    }
}}

struct raw_str_to_int_spirit
{
    int operator()(char const* str) const
    {
        char const* beg = str;
        char const* end = beg + strlen(str);
        int      result;

        if (boost::spirit::qi::parse(beg, end, boost::spirit::int_, result))
            if (beg == end) // ensure the whole string was parsed
                return result;

        return (BOOST_ASSERT(0), result);
    }
};

struct raw_str_to_int_lxcast
{
    int operator()(char const* str) const
    {
        return boost::lexical_cast<int>(str);
    }
};

struct raw_str_to_int_charconv
{
    int operator()(char const* str) const
    {
        char const* beg = str;
        char const* end = beg + strlen(str);
        int      result;

        const auto [ptr, ec] = std::from_chars(beg, end, result);
        if (ptr == end) // ensure the whole string was parsed
            return result;

        return (BOOST_ASSERT(0), result);
    }
};

template<typename Type, typename Converter>
double
raw_str_to(Converter const& cnv)
{
    auto strings = local::get_strs(); // Create strings on the stack
    int     size = strings.size();
    auto   timer = local::timer();

    for (int t = 0; t < local::num_cycles; ++t)
        for (int k = 0; k < size; ++k)
            local::sum += cnv(strings[k].c_str());

    return timer.value();
}

template<typename Type, typename Converter>
double
local::str_to(Converter const& try_converter)
{
    auto strings = local::get_strs(); // Create strings on the stack
    int     size = strings.size();
    auto   timer = local::timer();

    for (int t = 0; t < local::num_cycles; ++t)
        for (int k = 0; k < size; ++k)
            local::sum += boost::convert<Type>(strings[k].c_str(), try_converter).value();

    return timer.value();
}

template<typename string_type, typename Type, typename Converter>
double
local::to_str(Converter const& try_converter)
{
    auto values = local::get<Type>();
    int    size = values.size();
    auto  timer = local::timer();

    for (int t = 0; t < local::num_cycles; ++t)
        for (int k = 0; k < size; ++k)
            local::sum += *boost::convert<string_type>(Type(values[k]), try_converter).value().begin();

    return timer.value();
}

template<typename Converter>
double
performance_str_to_type(Converter const& try_converter)
{
    char const* input[] = { "no", "up", "dn" };
    local::timer  timer;

    for (int k = 0; k < local::num_cycles; ++k)
    {
        change chg = boost::convert<change>(input[k % 3], try_converter).value();
        int    res = chg.value();

        BOOST_TEST(res == k % 3);

        local::sum += res; // Make sure chg is not optimized out
    }
    return timer.value();
}

template<typename Converter>
double
performance_type_to_str(Converter const& try_converter)
{
    std::array<change, 3>   input = {{ change::no, change::up, change::dn }};
    std::array<string, 3> results = {{ "no", "up", "dn" }};
    local::timer            timer;

    for (int k = 0; k < local::num_cycles; ++k)
    {
        string res = boost::convert<string>(input[k % 3], try_converter).value();

        BOOST_TEST(res == results[k % 3]);

        local::sum += res[0]; // Make sure res is not optimized out
    }
    return timer.value();
}

template<typename Raw, typename Cnv>
void
performance_comparative(Raw const& raw, Cnv const& cnv, char const* txt)
{
    int const num_tries = 5;
    double     cnv_time = 0;
    double     raw_time = 0;

    for (int k = 0; k < num_tries; ++k) cnv_time += local::str_to<int>(cnv);
    for (int k = 0; k < num_tries; ++k) raw_time += raw_str_to<int>(raw);

    cnv_time /= num_tries;
    raw_time /= num_tries;

    double change = 100 * (1 - cnv_time / raw_time);

    printf("str-to-int: %s raw/cnv=%.2f/%.2f seconds (%.2f%%).\n", txt, raw_time, cnv_time, change);
}

int
main(int, char const* [])
{
    printf("Started performance tests...\n");

    printf("str-to-int: spirit/strtol/lcast/scanf/stream/charconv=%7.2f/%7.2f/%7.2f/%7.2f/%7.2f/%7.2f seconds.\n",
           local::str_to<int>(boost::cnv::spirit()),
           local::str_to<int>(boost::cnv::strtol()),
           local::str_to<int>(boost::cnv::lexical_cast()),
           local::str_to<int>(boost::cnv::printf()),
           local::str_to<int>(boost::cnv::cstream()),
           local::str_to<int>(boost::cnv::charconv()));
    printf("str-to-lng: spirit/strtol/lcast/scanf/stream/charconv=%7.2f/%7.2f/%7.2f/%7.2f/%7.2f/%7.2f seconds.\n",
           local::str_to<long int>(boost::cnv::spirit()),
           local::str_to<long int>(boost::cnv::strtol()),
           local::str_to<long int>(boost::cnv::lexical_cast()),
           local::str_to<long int>(boost::cnv::printf()),
           local::str_to<long int>(boost::cnv::cstream()),
           local::str_to<long int>(boost::cnv::charconv()));
    printf("str-to-dbl: spirit/strtol/lcast/scanf/stream/charconv=%7.2f/%7.2f/%7.2f/%7.2f/%7.2f/%7.2f seconds.\n",
           local::str_to<double>(boost::cnv::spirit()),
           local::str_to<double>(boost::cnv::strtol()),
           local::str_to<double>(boost::cnv::lexical_cast()),
           local::str_to<double>(boost::cnv::printf()),
           local::str_to<double>(boost::cnv::cstream()),
           local::str_to<double>(boost::cnv::charconv()));

    printf("int-to-str: spirit/strtol/lcast/prntf/stream/charconv=%7.2f/%7.2f/%7.2f/%7.2f/%7.2f/%7.2f seconds.\n",
           local::to_str<std::string, int>(boost::cnv::spirit()),
           local::to_str<std::string, int>(boost::cnv::strtol()),
           local::to_str<std::string, int>(boost::cnv::lexical_cast()),
           local::to_str<std::string, int>(boost::cnv::printf()),
           local::to_str<std::string, int>(boost::cnv::cstream()),
           local::to_str<std::string, int>(boost::cnv::charconv()));
    printf("lng-to-str: spirit/strtol/lcast/prntf/stream/charconv=%7.2f/%7.2f/%7.2f/%7.2f/%7.2f/%7.2f seconds.\n",
           local::to_str<std::string, long int>(boost::cnv::spirit()),
           local::to_str<std::string, long int>(boost::cnv::strtol()),
           local::to_str<std::string, long int>(boost::cnv::lexical_cast()),
           local::to_str<std::string, long int>(boost::cnv::printf()),
           local::to_str<std::string, long int>(boost::cnv::cstream()),
           local::to_str<std::string, long int>(boost::cnv::charconv()));
    printf("dbl-to-str: spirit/strtol/lcast/prntf/stream/charconv=%7.2f/%7.2f/%7.2f/%7.2f/%7.2f/%7.2f seconds.\n",
           local::to_str<std::string, double>(boost::cnv::spirit()),
           local::to_str<std::string, double>(boost::cnv::strtol()(arg::precision = 6)),
           local::to_str<std::string, double>(boost::cnv::lexical_cast()),
           local::to_str<std::string, double>(boost::cnv::printf()(arg::precision = 6)),
           local::to_str<std::string, double>(boost::cnv::cstream()(arg::precision = 6)),
           local::to_str<std::string, double>(boost::cnv::charconv()(arg::precision = 6)));

    printf("str-to-user-type: lcast/stream/strtol/charconv=%.2f/%.2f/%.2f/%.2f seconds.\n",
           performance_str_to_type(boost::cnv::lexical_cast()),
           performance_str_to_type(boost::cnv::cstream()),
           performance_str_to_type(boost::cnv::strtol()),
           performance_str_to_type(boost::cnv::charconv()));
    printf("user-type-to-str: lcast/stream/strtol/charconv=%.2f/%.2f/%.2f/%.2f seconds.\n",
           performance_type_to_str(boost::cnv::lexical_cast()),
           performance_type_to_str(boost::cnv::cstream()),
           performance_type_to_str(boost::cnv::strtol()),
           performance_type_to_str(boost::cnv::charconv()));

    //[small_string_results
    printf("strtol int-to std::string/small-string: %.2f/%.2f seconds.\n",
           local::to_str<std::string, int>(boost::cnv::strtol()),
           local::to_str<  my_string, int>(boost::cnv::strtol()));
    printf("spirit int-to std::string/small-string: %.2f/%.2f seconds.\n",
           local::to_str<std::string, int>(boost::cnv::spirit()),
           local::to_str<  my_string, int>(boost::cnv::spirit()));
    printf("stream int-to std::string/small-string: %.2f/%.2f seconds.\n",
           local::to_str<std::string, int>(boost::cnv::cstream()),
           local::to_str<  my_string, int>(boost::cnv::cstream()));
    printf("charconv int-to std::string/small-string: %.2f/%.2f seconds.\n",
           local::to_str<std::string, int>(boost::cnv::charconv()),
           local::to_str<  my_string, int>(boost::cnv::charconv()));
    //]
    performance_comparative(raw_str_to_int_spirit(), boost::cnv::spirit(),       "spirit");
    performance_comparative(raw_str_to_int_lxcast(), boost::cnv::lexical_cast(), "lxcast");
    performance_comparative(raw_str_to_int_charconv(), boost::cnv::charconv(), "charconv");

    return boost::report_errors();
}

#endif