summaryrefslogtreecommitdiffstats
path: root/tests/benchmarks/corelib/text/qregularexpression/tst_bench_qregularexpression.cpp
blob: 83c87be0b1513bc0bc00a5cf3a9f7917056cb36d (plain)
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
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QRegularExpression>
#include <QTest>

/*!
    \internal
    The main idea of the benchmark is to compare performance of QRE classes
    before and after any changes in the implementation.
    It does not try to compare performance between different patterns or
    matching options.
*/

static const QString textToMatch { "The quick brown fox jumped over the lazy dogs" };
static const QString nonEmptyPattern { "(?<article>\\w+) (?<noun>\\w+)" };
static const QRegularExpression::PatternOptions nonEmptyPatternOptions {
    QRegularExpression::CaseInsensitiveOption | QRegularExpression::DotMatchesEverythingOption
};

class tst_QRegularExpressionBenchmark : public QObject
{
    Q_OBJECT

private slots:
    void createDefault();
    void createAndMoveDefault();

    void createCustom();
    void createAndMoveCustom();

    void matchDefault();
    void matchDefaultOptimized();

    void matchCustom();
    void matchCustomOptimized();

    void globalMatchDefault();
    void globalMatchDefaultOptimized();

    void globalMatchCustom();
    void globalMatchCustomOptimized();

    void queryMatchResults();
    void queryMatchResultsByGroupIndex();
    void queryMatchResultsByGroupName();
    void iterateThroughGlobalMatchResults();
};

void tst_QRegularExpressionBenchmark::createDefault()
{
    QBENCHMARK {
        QRegularExpression re;
        Q_UNUSED(re);
    }
}

void tst_QRegularExpressionBenchmark::createAndMoveDefault()
{
    QBENCHMARK {
        QRegularExpression re;
        // We can compare to results of previous test to roughly
        // estimate the cost for the move() call.
        QRegularExpression re2(std::move(re));
        Q_UNUSED(re2);
    }
}

void tst_QRegularExpressionBenchmark::createCustom()
{
    QBENCHMARK {
        QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
        Q_UNUSED(re);
    }
}

void tst_QRegularExpressionBenchmark::createAndMoveCustom()
{
    QBENCHMARK {
        QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
        // We can compare to results of previous test to roughly
        // estimate the cost for the move() call.
        QRegularExpression re2(std::move(re));
        Q_UNUSED(re2);
    }
}

/*!
    \internal This benchmark measures the performance of the match() together
    with pattern compilation for a default-constructed object.
    We need to create the object every time, so that the compiled pattern
    does not get cached.
*/
void tst_QRegularExpressionBenchmark::matchDefault()
{
    QBENCHMARK {
        QRegularExpression re;
        auto matchResult = re.match(textToMatch);
        Q_UNUSED(matchResult);
    }
}

/*!
    \internal This benchmark measures the performance of the match() without
    pattern compilation for a default-constructed object.
    The pattern is precompiled before the actual benchmark starts.

    \note In case we make the default constructor non-allocating, the results
    of the benchmark will be very close to the unoptimized one, as it will have
    to compile the pattern inside the call to match().
*/
void tst_QRegularExpressionBenchmark::matchDefaultOptimized()
{
    QRegularExpression re;
    re.optimize();
    QBENCHMARK {
        auto matchResult = re.match(textToMatch);
        Q_UNUSED(matchResult);
    }
}

/*!
    \internal This benchmark measures the performance of the match() together
    with pattern compilation for an object with custom pattern and pattern
    options.
    We need to create the object every time, so that the compiled pattern
    does not get cached.
*/
void tst_QRegularExpressionBenchmark::matchCustom()
{
    QBENCHMARK {
        QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
        auto matchResult = re.match(textToMatch);
        Q_UNUSED(matchResult);
    }
}

/*!
    \internal This benchmark measures the performance of the match() without
    pattern compilation for an object with custom pattern and pattern
    options.
    The pattern is precompiled before the actual benchmark starts.
*/
void tst_QRegularExpressionBenchmark::matchCustomOptimized()
{
    QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
    re.optimize();
    QBENCHMARK {
        auto matchResult = re.match(textToMatch);
        Q_UNUSED(matchResult);
    }
}

/*!
    \internal This benchmark measures the performance of the globalMatch()
    together with the pattern compilation for a default-constructed object.
    We need to create the object every time, so that the compiled pattern
    does not get cached.
*/
void tst_QRegularExpressionBenchmark::globalMatchDefault()
{
    QBENCHMARK {
        QRegularExpression re;
        auto matchResultIterator = re.globalMatch(textToMatch);
        Q_UNUSED(matchResultIterator);
    }
}

/*!
    \internal This benchmark measures the performance of the globalMatch()
    without the pattern compilation for a default-constructed object.
    The pattern is precompiled before the actual benchmark starts.

    \note In case we make the default constructor non-allocating, the results
    of the benchmark will be very close to the unoptimized one, as it will have
    to compile the pattern inside the call to globalMatch().
*/
void tst_QRegularExpressionBenchmark::globalMatchDefaultOptimized()
{
    QRegularExpression re;
    re.optimize();
    QBENCHMARK {
        auto matchResultIterator = re.globalMatch(textToMatch);
        Q_UNUSED(matchResultIterator);
    }
}

/*!
    \internal This benchmark measures the performance of the globalMatch()
    together with the pattern compilation for an object with custom pattern
    and pattern options.
    We need to create the object every time, so that the compiled pattern
    does not get cached.
*/
void tst_QRegularExpressionBenchmark::globalMatchCustom()
{
    QBENCHMARK {
        QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
        auto matchResultIterator = re.globalMatch(textToMatch);
        Q_UNUSED(matchResultIterator);
    }
}

/*!
    \internal This benchmark measures the performance of the globalMatch()
    without the pattern compilation for an object with custom pattern and
    pattern options.
    The pattern is precompiled before the actual benchmark starts.
*/
void tst_QRegularExpressionBenchmark::globalMatchCustomOptimized()
{
    QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
    re.optimize();
    QBENCHMARK {
        auto matchResultIterator = re.globalMatch(textToMatch);
        Q_UNUSED(matchResultIterator);
    }
}

void tst_QRegularExpressionBenchmark::queryMatchResults()
{
    QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
    auto matchResult = re.match(textToMatch);
    QBENCHMARK {
        auto texts = matchResult.capturedTexts();
        Q_UNUSED(texts);
    }
}

void tst_QRegularExpressionBenchmark::queryMatchResultsByGroupIndex()
{
    QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
    auto matchResult = re.match(textToMatch);
    const int capturedCount = matchResult.lastCapturedIndex();
    QBENCHMARK {
        for (int i = 0, imax = capturedCount; i < imax; ++i) {
            auto result = matchResult.capturedView(i);
            Q_UNUSED(result);
        }
    }
}

void tst_QRegularExpressionBenchmark::queryMatchResultsByGroupName()
{
    QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
    auto matchResult = re.match(textToMatch);
    const auto groups = matchResult.regularExpression().namedCaptureGroups();
    QBENCHMARK {
        for (const QString &groupName : groups) {
            // introduce this checks to get rid of tons of warnings.
            // The result for empty string is always the same.
            if (!groupName.isEmpty()) {
                auto result = matchResult.capturedView(groupName);
                Q_UNUSED(result);
            }
        }
    }
}

void tst_QRegularExpressionBenchmark::iterateThroughGlobalMatchResults()
{
    QRegularExpression re(nonEmptyPattern, nonEmptyPatternOptions);
    auto matchResultIterator = re.globalMatch(textToMatch);
    QBENCHMARK {
        if (matchResultIterator.isValid()) {
            while (matchResultIterator.hasNext()) {
                matchResultIterator.next();
            }
        }
    }
}

QTEST_MAIN(tst_QRegularExpressionBenchmark)

#include "tst_bench_qregularexpression.moc"