aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/benchmarks/lexer.cpp
blob: 5b0612fc9b0be18fbb0f8f16a93fd033f9c9a2a8 (plain) (blame)
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
// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "math-server" project.
// For details, see https://github.com/egor-tensin/math-server.
// Distributed under the MIT License.

#include <server/lexer/details/parse.hpp>

#include <benchmark/benchmark.h>

#include <string_view>
#include <vector>

// I noticed std::regex_search cropping up in profiling results.
// Switching to boost::regex_search yielded a huge benefit: sometimes a 15x
// increase in regex matching (on VS builds in particular).
// Should be easily reproducible using these micro-benchmarks.

namespace {

class NumberExamples : public benchmark::Fixture {
protected:
    std::vector<std::string_view> m_numbers{
        "0",
        "123",
        "0.123",
        ".123",
        "1e9",
        "1.87E-18",
        "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789",
    };
};

class WhitespaceExamples : public benchmark::Fixture {
protected:
    std::vector<std::string_view> m_whitespace{
        "",
        "  1",
        "                                                                                                                                123",
    };
};

} // namespace

BENCHMARK_F(NumberExamples, StdParseNumber)(benchmark::State& state) {
    using namespace math::server::lexer::details;
    for (auto _ : state) {
        for (const auto& src : m_numbers) {
            impl::std_parse_number(src);
        }
    }
}

BENCHMARK_F(NumberExamples, BoostParseNumber)(benchmark::State& state) {
    using namespace math::server::lexer::details;
    for (auto _ : state) {
        for (const auto& src : m_numbers) {
            impl::boost_parse_number(src);
        }
    }
}

BENCHMARK_F(WhitespaceExamples, StdParseWhitespace)(benchmark::State& state) {
    using namespace math::server::lexer::details;
    for (auto _ : state) {
        for (const auto& src : m_whitespace) {
            impl::std_parse_whitespace(src);
        }
    }
}

BENCHMARK_F(WhitespaceExamples, BoostParseWhitespace)(benchmark::State& state) {
    using namespace math::server::lexer::details;
    for (auto _ : state) {
        for (const auto& src : m_whitespace) {
            impl::boost_parse_whitespace(src);
        }
    }
}