File: performance_spirit.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (107 lines) | stat: -rw-r--r-- 3,268 bytes parent folder | download | duplicates (9)
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
// 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.

// This code has been adapted from libs/spirit/optimization/qi/int_parser.cpp.
// This code uses the performance testing framework from libs/spirit/optimization/measure.cpp.
// See these mentioned files for the copyright notice.

#include "./test.hpp"

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

#include "./prepare.hpp"
#include <boost/convert.hpp>
#include <boost/convert/spirit.hpp>
#include <boost/convert/strtol.hpp>
#include <boost/convert/lexical_cast.hpp>

#include <libs/spirit/workbench/measure.hpp>
#include <vector>

namespace
{
    namespace local
    {
        struct base : test::base
        {
            base() : strings_(local::get_strs()) {}

            // Test strings are created as part of the object, i.e. on the stack to make sure
            // they are easily accessed.
            local::strings strings_;
        };
    }
    struct raw_lxcast_str_to_int_test : local::base
    {
        void benchmark()
        {
            for (size_t i = 0; i < strings_.size(); ++i)
                this->val += boost::lexical_cast<int>(strings_[i].c_str());
        }
    };
    struct cnv_lxcast_str_to_int_test : local::base
    {
        void benchmark()
        {
            for (size_t i = 0; i < strings_.size(); ++i)
                this->val += boost::convert<int>(strings_[i].c_str(), cnv).value();
        }
        boost::cnv::lexical_cast cnv;
    };
    struct raw_spirit_str_to_int_test : local::base
    {
        static int parse(char const* str)
        {
            char const* beg = str;
            char const* end = beg + strlen(str);
            int n;

            if (boost::spirit::qi::parse(beg, end, boost::spirit::qi::int_, n))
                if (beg == end)
                    return n;

            return (BOOST_ASSERT(0), 0);
        }
        void benchmark()
        {
            for (size_t i = 0; i < strings_.size(); ++i)
                this->val += parse(strings_[i].c_str());
        }
    };
    struct cnv_spirit_str_to_int_test : local::base
    {
        void benchmark()
        {
            for (size_t i = 0; i < strings_.size(); ++i)
                this->val += boost::convert<int>(strings_[i].c_str(), cnv).value();
        }
        boost::cnv::spirit cnv;
    };
}

int
main(int, char const* [])
{
    // This code has been adapted from libs/spirit/optimization/qi/int_parser.cpp.
    // This code uses the performance testing framework from libs/spirit/optimization/measure.cpp.
    // See these mentioned files for the copyright notice.

    BOOST_SPIRIT_TEST_BENCHMARK(
        10000000, // This is the maximum repetitions to execute
        (raw_lxcast_str_to_int_test)
        (cnv_lxcast_str_to_int_test)
        (raw_spirit_str_to_int_test)
        (cnv_spirit_str_to_int_test)
    )

    // This is ultimately responsible for preventing all the test code
    // from being optimized away.  Change this to return 0 and you
    // unplug the whole test's life support system.
    return test::live_code != 0;
}

#endif